Treasure Exploration(二分最大匹配+floyd)

Treasure Exploration

Time Limit: 6000MS   Memory Limit: 65536K
Total Submissions: 7455   Accepted: 3053

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题解:需要加上floyd,用上vector 竟然re了;ac代码:
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 #define mem(x,y) memset(x,y,sizeof(x))
 7 const int MAXN=510;
 8 int mp[MAXN][MAXN],vis[MAXN],usd[MAXN];
 9 int N;
10 bool dfs(int u){
11     for(int i=1;i<=N;i++){
12         if(!vis[i]&&mp[u][i]){
13             vis[i]=1;
14             if(!usd[i]||dfs(usd[i])){
15                 usd[i]=u;return true;
16             }
17         }
18     }
19     return false;
20 }
21 void floyd(){
22     for(int i=1;i<=N;i++)
23         for(int j=1;j<=N;j++)
24             if(mp[i][j]==0){
25                 for(int k=1;k<=N;k++)
26                     if(mp[i][k]&&mp[k][j])
27                         mp[i][j]=1;
28             }
29 }
30 int main(){
31     int M;
32     while(scanf("%d%d",&N,&M),N|M){
33             int a,b;
34             mem(mp,0);mem(usd,0);
35         while(M--){
36             scanf("%d%d",&a,&b);
37             mp[a][b]=1;
38         }
39         int ans=0;
40         floyd();
41         for(int i=1;i<=N;i++){
42                 mem(vis,0);
43             if(dfs(i))ans++;
44         }
45         printf("%d\n",N-ans);
46     }
47     return 0;
48 }

re代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<vector>
 6 using namespace std;
 7 #define mem(x,y) memset(x,y,sizeof(x))
 8 const int MAXN=510;
 9 int vis[MAXN],usd[MAXN];
10 vector<int>vec[MAXN];
11 int N;
12 bool dfs(int u){
13     for(int i=0;i<vec[u].size();i++){
14             int v=vec[u][i];
15         if(!vis[v]){
16             vis[v]=1;
17             if(!usd[v]||dfs(usd[v])){
18                 usd[v]=u;return true;
19             }
20         }
21     }
22     return false;
23 }
24 void floyd(){
25     for(int i=1;i<=N;i++){
26         for(int k=0;k<vec[i].size();k++)
27         for(int j=0;j<vec[vec[i][k]].size();j++){
28             vec[i].push_back(vec[vec[i][k]][j]);
29         }
30     }
31 }
32 int main(){
33     int M;
34     while(scanf("%d%d",&N,&M),N|M){
35             int a,b;
36             mem(usd,0);
37             for(int i=0;i<=N;i++)vec[i].clear();
38         while(M--){
39             scanf("%d%d",&a,&b);
40            vec[a].push_back(b);
41         }
42         floyd();
43         int ans=0;
44         for(int i=1;i<=N;i++){
45                 mem(vis,0);
46             if(dfs(i))ans++;
47         }
48         printf("%d\n",N-ans);
49     }
50     return 0;
51 }
时间: 2024-08-23 07:02:15

Treasure Exploration(二分最大匹配+floyd)的相关文章

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

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——————【最小路径覆盖、可重点、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

POJ2594:Treasure Exploration(Floyd + 最小路径覆盖)

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

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

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

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

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

POJ2594 Treasure Exploration[DAG的最小可相交路径覆盖]

Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 8301   Accepted: 3402 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(最小路径覆盖变形)

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

POJ Treasure Exploration 【DAG交叉最小路径覆盖】

传送门:http://poj.org/problem?id=2594 Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 9802   Accepted: 3979 Description Have you ever read any book about treasure exploration? Have you ever see any film about treasure e