码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-11-10 15:41:56      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:

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 }

 

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

标签:

原文地址:http://www.cnblogs.com/handsomecui/p/4952997.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!