poj 2594 Treasure Exploration (二分匹配)

Treasure Exploration

Time Limit: 6000MS   Memory Limit: 65536K
Total Submissions: 6558   Accepted: 2644

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

Source

POJ Monthly--2005.08.28,Li Haoyuan

最小路径覆盖(非最小边覆盖):

使用最少的边把所有点覆盖=原点数-最大匹配(修改后的图)

注意一种情况:

5  4

1-->3

2-->3

3-->4

3-->5

这里其实用2个robots就行了,但未处理过的图得出来的结果却不是,可见这题是要把图处理一下的,我用了floyd,感觉O(n^3)是会TLE的,都是没有。

 1 //1144K    969MS    C++    1024B    2014-06-14 09:27:31
 2 #include<stdio.h>
 3 #include<string.h>
 4 #define N 505
 5 int g[N][N];
 6 int match[N];
 7 int vis[N];
 8 int n,m;
 9 void floyd()
10 {
11     for(int k=1;k<=n;k++)
12         for(int i=1;i<=n;i++)
13             for(int j=1;j<=n;j++)
14                 if(g[i][k] && g[k][j])
15                     g[i][j]=1;
16 }
17 int dfs(int u)
18 {
19     for(int i=1;i<=n;i++)
20         if(!vis[i] && g[u][i]){
21             vis[i]=1;
22             if(match[i]==-1 || dfs(match[i])){
23                 match[i]=u;
24                 return 1;
25             }
26         }
27     return 0;
28 }
29 int hungary()
30 {
31     int ret=0;
32     memset(match,-1,sizeof(match));
33     for(int i=1;i<=n;i++){
34         memset(vis,0,sizeof(vis));
35         ret+=dfs(i);
36     }
37     return ret;
38 }
39 int main(void)
40 {
41     int a,b;
42     while(scanf("%d%d",&n,&m)!=EOF && (n+m))
43     {
44         memset(g,0,sizeof(g));
45         for(int i=0;i<m;i++){
46             scanf("%d%d",&a,&b);
47             g[a][b]=1;
48         }
49         floyd();
50         printf("%d\n",n-hungary());
51     }
52     return 0;
53 }

poj 2594 Treasure Exploration (二分匹配),布布扣,bubuko.com

时间: 2024-10-20 16:18:13

poj 2594 Treasure Exploration (二分匹配)的相关文章

poj 2594 Treasure Exploration 二分图匹配

点击打开链接题目链接 Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7215   Accepted: 2947 Description Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploration? Have you eve

POJ - 2594 Treasure Exploration 二分图匹配 + floyd

题目大意:在火星上有N个矿,有点矿之间存在着一条路,由于在火星比较特殊,该路变成了单向路,且机器人只能出现在这条路的两个端点,问最少需要派多少机器人,才能探清这些矿 解题思路:路可以拼接起来形成一条新的路,所以在所给的条件下还可以再扩展,用floyd将所有能连通的点找出来 接下来就是二分匹配的过程了,求出最大匹配数,在用n-最大匹配数就是答案了 #include<cstdio> #include<cstring> #include<vector> using names

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

POJ 2594 Treasure Exploration 题目链接 题意:有向无环图,求最少多少条路径能够覆盖整个图,点能够反复走 思路:和普通的最小路径覆盖不同的是,点能够反复走,那么事实上仅仅要在多一步.利用floyd求出传递闭包.然后依据这个新的图去做最小路径覆盖就可以 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using names

Poj 2594 Treasure Exploration (最小边覆盖+传递闭包)

题目链接: Poj 2594 Treasure Exploration 题目描述: 在外星上有n个点需要机器人去探险,有m条单向路径.问至少需要几个机器人才能遍历完所有的点,一个点可以被多个机器人经过. 解题思路: 一眼看上去是普通的最小边覆盖,但是仔细想后发现如果在原图上进行最大匹配的话,每个点只能经过一次.这样的话对于本题求出的并不是最优解,所以我们要先对原图进行传递闭包处理,然后再进行最大匹配. 这个题目点数太少H_K和匈牙利算法在空间和时间上并没有什么差,就代码复杂度而言匈牙利算法更有优

poj 2594 Treasure Exploration(最小路径覆盖/二分最大匹配)

Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7208   Accepted: 2944 Description Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploration? Have you ever explored

poj 2594 Treasure Exploration 最少边覆盖+传递闭包

给出一个有向无环图,问最少多少条边就可以覆盖所有的点,很明显的最少路径覆盖问题,但是点可以重复用,这与hungary算法不太一样,我们可以这样处理,假设a要经过b点到c,但是b点已经被访问过,为了保证a顺利到c,就让a直接飞过b到c,也就是说添加一条a到c的边,这其实就是求传递闭包,我们可以通过floyd算法求出.然后ans=n-hungary(). 一开始用了邻接表的最大二分匹配,在求闭包的时候用的dfs,但是超时了,估计是求闭包的时候加边过多导致的,而且还要判断边是否重复,因此在时空复杂度允

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

Treasure Exploration Time Limit:6000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2594 Description Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploratio

POJ 2594 Treasure Exploration(带交叉路的最小路径覆盖)

题意: 派机器人去火星寻宝,给出一个无环的有向图,机器人可以降落在任何一个点上,再沿着路去其他点探索,我们的任务是计算至少派多少机器人就可以访问到所有的点.有的点可以重复去. 输入数据: 首先是n和m, 代表有n个顶点, m条边.(m和n同时为0时则输入数据结束) 接下来m行,每行两个数字 a, b代表 从a到b可以通行. 题目分析: 这道题目与最小路径有一点差别,最小路径覆盖上是不存在交叉路的,但是这个题目是存在交叉路的. 对于交叉路的处理我们可以使用Floyd闭包传递.即 i->j, j->

POJ 2594 Treasure Exploration 最小可相交路径覆盖

最小路径覆盖 DAG的最小可相交路径覆盖: 算法:先用floyd求出原图的传递闭包,即如果a到b有路径,那么就加边a->b.然后就转化成了最小不相交路径覆盖问题. 这里解释一下floyd的作用如果1->2->3->4那么1可以到达2,3,4只要需要借助一些点,那么就可以直接把1与2,3,4相连,这就是floyd要做的事. 证明:为了连通两个点,某条路径可能经过其它路径的中间点.比如1->3->4,2->4->5.但是如果两个点a和b是连通的,只不过中间需要经