Treasure Exploration

Time Limit: 6000MS   Memory Limit: 65536K
Total Submissions: 9794   Accepted: 3975

题目链接http://poj.org/problem?id=2594

Description:

Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploration? Have you ever explored treasure? If you never have such experiences, you would never know what fun treasure exploring brings to you.
Recently, a company named EUC (Exploring the Unknown Company) plan to explore an unknown place on Mars, which is considered full of treasure. For fast development of technology and bad environment for human beings, EUC sends some robots to explore the treasure.

To make it easy, we use a graph, which is formed by N points (these N points are numbered from 1 to N), to represent the places to be explored. And some points are connected by one-way road, which means that, through the road, a robot can only move from one end to the other end, but cannot move back. For some unknown reasons, there is no circle in this graph. The robots can be sent to any point from Earth by rockets. After landing, the robot can visit some points through the roads, and it can choose some points, which are on its roads, to explore. You should notice that the roads of two different robots may contain some same point.

For financial reason, EUC wants to use minimal number of robots to explore all the points on Mars.

As an ICPCer, who has excellent programming skill, can your help EUC?

Input:

The input will consist of several test cases. For each test case, two integers N (1 <= N <= 500) and M (0 <= M <= 5000) are given in the first line, indicating the number of points and the number of one-way roads in the graph respectively. Each of the following M lines contains two different integers A and B, indicating there is a one-way from A to B (0 < A, B <= N). The input is terminated by a single line with two zeros.

Output:

For each test of the input, print a line containing the least robots needed.

Sample Input:

1 0
2 1
1 2
2 0
0 0

Sample Output:

1
1
2

题意

问有向图里,需要最少多少个机器人,能走遍所有的点,并且可以走其它机器人已经走过的路径。

题解:

这道题我一开始以为就是最小路径覆盖,结果一直WA...

但其实这题和最小路径覆盖有区别,最小路径覆盖的要求就是在图中找尽量少的路径,让每个节点恰好在一条路径上,单独的一个结点也可以作为一条路径。但是,正是因为每个结点在一条路径上,所以没有重复的路径。

但是这题路径是可以重复的,所以可以利用floyd传递闭包,让原来被“阻断”的路径可以链接起来。

推荐一下这篇博客,挺好的,讲的挺详细的:https://www.cnblogs.com/justPassBy/p/5369930.html

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define mem(x) memset(x,0,sizeof(x))
using namespace std; const int N = ;
int map[N][N],check[N<<],match[N<<],link[N][N<<];
int n,m,ans; inline int dfs(int x){
for(int i=n+;i<=*n;i++){
if(link[x][i] && !check[i]){
check[i]=;
if(!match[i] || dfs(match[i])){
match[i]=x;
return ;
}
}
}
return ;
} int main(){
while(scanf("%d%d",&n,&m)){
if(!n && !m) break ;
mem(map);mem(match);mem(link);ans=;
for(int i=,x,y;i<=m;i++){
scanf("%d%d",&x,&y);
map[x][y]=;
}
for(int k=;k<=n;k++){
for(int i=;i<=n;i++){
if(map[i][k])
for(int j=;j<=n;j++){
if(map[k][j]) map[i][j]=;
}
}
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(map[i][j]) link[i][j+n]=;
for(int i=;i<=n;i++){
mem(check);
if(dfs(i)) ans++;
}
printf("%d\n",n-ans);
}
return ;
}

POJ2594:Treasure Exploration(Floyd + 最小路径覆盖)的更多相关文章

  1. POJ2594 Treasure Exploration(最小路径覆盖)

    Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 8550   Accepted: 3 ...

  2. POJ 2594 Treasure Exploration (Floyd+最小路径覆盖)

    <题目链接> 题目大意: 机器人探索宝藏,有N个点,M条边.问你要几个机器人才能遍历所有的点. 解题分析: 刚开始还以为是最小路径覆盖的模板题,但是后面才知道,本题允许一个点经过多次,这与 ...

  3. [POJ2594] Treasure Exploration(最小路径覆盖-传递闭包 + 匈牙利算法)

    传送门 引子: 有一个问题,是对于一个图上的所有点,用不相交的路径把他们覆盖,使得每个点有且仅属于一条路径,且这个路径数量尽量小. 对于这个问题可以把直接有边相连的两点 x —> y,建一个二分 ...

  4. POJ 2594 —— Treasure Exploration——————【最小路径覆盖、可重点、floyd传递闭包】

    Treasure Exploration Time Limit:6000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64 ...

  5. POJ 2594 Treasure Exploration(最小路径覆盖变形)

    POJ 2594 Treasure Exploration 题目链接 题意:有向无环图,求最少多少条路径能够覆盖整个图,点能够反复走 思路:和普通的最小路径覆盖不同的是,点能够反复走,那么事实上仅仅要 ...

  6. poj 2594 Treasure Exploration(最小路径覆盖,可重点)

    题意:选出最小路径覆盖图中所有点,路径可以交叉,也就是允许路径有重复的点. 分析:这个题的难点在于如何解决有重复点的问题-方法就是使用Floyd求闭包,就是把间接相连的点直接连上边,然后就是求最小路径 ...

  7. POJ-2594 Treasure Exploration,floyd+最小路径覆盖!

                                                 Treasure Exploration 复见此题,时隔久远,已忘,悲矣! 题意:用最少的机器人沿单向边走完( ...

  8. POJ-2594 Treasure Exploration floyd传递闭包+最小路径覆盖,nice!

    Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 8130   Accepted: 3 ...

  9. Treasure Exploration---poj2594(传递闭包Floyd+最小路径覆盖)

    题目链接:http://poj.org/problem?id=2594 在外星上有n个点需要机器人去探险,有m条单向路径.问至少需要几个机器人才能遍历完所有的点,一个点可以被多个机器人经过(这就是和单 ...

随机推荐

  1. Scrapy进阶

    当我们使用scrapy框架爬取网站的时候,我们会有一个入口的url,一个名为start_urls,我们爬取的第一个网页是从这一开始的. 需求: 现在我们有一个这样的需求,比如说我们对起始的URL有一个 ...

  2. dedecms织梦首页被篡改 网站被黑被跳转的解决办法建议

    2018年的中秋节即将来临,我们Sine安全公司,最近接到很多用dedecms程序的企业公司网站客户的反馈,说是公司网站经常被篡改,包括网站首页的标题内容以及描述内容,都被改成了什么北京赛车,北京PK ...

  3. hadoop jar x.jar 执行过程

    hadoop jar  x.jar  执行过程 Yarn框架执行内容 1,job.waitforcompletion() 启动 Runjar 进程  -> Resourcemanage申请一个j ...

  4. 两种方法实现Python二分查找算法

    两种方法实现Python二分查找算法   一. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 arr=[1,3,6,9,10,20,30] def findnumber( ...

  5. Android TextView 单行文本的坑

    这是android系统的一个bug,描述如下:https://code.google.com/p/android/issues/detail?id=33868 具体来说就是当一个TextView设置了 ...

  6. 云计算之路-阿里云上:原来“黑色0.1秒”发生在socket读取数据时

    在昨天的博文(云计算之路-阿里云上:读取缓存时的“黑色0.1秒”)中我们犯了一个很低级的错误——把13ms算成了130ms(感谢陈硕发现这个错误!),从而对问题的原因作出了错误的推断,望大家谅解! 从 ...

  7. 【python模块】——logging

    python学习——logging模块

  8. 「个人训练」Can you solve this equation?(HDU-2199)

    题意与分析 纯粹水题.本来想做下放松心情的,结果还是被坑了qaq 重点就是在浮点误差.比较左右的下次就直接上1e-10,别看着题目说1e-4然后给个-5,结果暴wa.气傻了..... 代码 #incl ...

  9. restAssured + TestNG测试接口,以下是一个get 请求。

    package Elaine.Test.G.APITest; import org.testng.Assert;import org.testng.annotations.BeforeTest;imp ...

  10. DOM操作相关案例 模态对话框,简易留言板,js模拟选择器hover,tab选项卡,购物车案例

    1.模态框案例 需求: 打开网页时有一个普通的按钮,点击当前按钮显示一个背景图,中心并弹出一个弹出框,点击X的时候会关闭当前的模态框 代码如下: <!DOCTYPE html> <h ...