Poj 2594 Treasure Exploration【可相交最小路径覆盖】

Treasure Exploration
Time Limit: 6000MS Memory Limit: 65536K
Total Submissions: 8537 Accepted: 3487

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


题目大意:

给你N个点,M条有向边,问你一共在地图上放置最小个数的机器人,使得其能够访问到所有的点(允许点重复走)。

求这个最小数量。


思路:


作为队伍中深攻图论选手,竟然没有做过克香蕉最小路径覆盖的问题,耻辱啊,耻辱= =


问题就是一个最小路径覆盖的问题,不过这里允许点可以重复走.所以并不能直接建图跑二分匹配,我们要先跑一下Floyd求传递闭包,然后根据得到的邻接矩阵来建图。

最小路径覆盖数=n-最大二分匹配数。


Ac代码:

[cpp]  view plain  copy
 print ?
  1. #include<stdio.h>  
  2. #include<string.h>  
  3. #include<iostream>  
  4. using namespace std;  
  5. int mp[505][505];  
  6. int match[551];  
  7. int vis[551];  
  8. int n,m;  
  9. void Floyd()  
  10. {  
  11.     for(int i=0;i<n;i++)  
  12.     {  
  13.         for(int j=0;j<n;j++)  
  14.         {  
  15.             for(int k=0;k<n;k++)  
  16.             {  
  17.                 if(mp[j][i]==1&&mp[i][k]==1)mp[j][k]=1;  
  18.             }  
  19.         }  
  20.     }  
  21. }  
  22. int find(int u)  
  23. {  
  24.     for(int i=0;i<n;i++)  
  25.     {  
  26.         if(i==u)continue;  
  27.         if(mp[u][i]==1&&vis[i]==0)  
  28.         {  
  29.             vis[i]=1;  
  30.             if(match[i]==-1||find(match[i]))  
  31.             {  
  32.                 match[i]=u;  
  33.                 return 1;  
  34.             }  
  35.         }  
  36.     }  
  37.     return 0;  
  38. }  
  39. void Slove()  
  40. {  
  41.     int sum=0;  
  42.     memset(match,-1,sizeof(match));  
  43.     for(int i=0;i<n;i++)  
  44.     {  
  45.         memset(vis,0,sizeof(vis));  
  46.         if(find(i))sum++;  
  47.     }  
  48.     printf("%d\n",n-sum);  
  49. }  
  50. int main()  
  51. {  
  52.     while(~scanf("%d%d",&n,&m))  
  53.     {  
  54.         if(n==0&&m==0)break;  
  55.         memset(mp,0,sizeof(mp));  
  56.         for(int i=0;i<m;i++)  
  57.         {  
  58.             int x,y;  
  59.             scanf("%d%d",&x,&y);  
  60.             x--;y--;  
  61.             mp[x][y]=1;  
  62.         }  
  63.         Floyd();  
  64.         Slove();  
  65.     }  
  66. }  








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值