Treasure Exploration POJ - 2594 有向图路径可相交的最小路径覆盖模板题

Posted pengge666

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Treasure Exploration POJ - 2594 有向图路径可相交的最小路径覆盖模板题相关的知识,希望对你有一定的参考价值。

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
题意:在外星上有n个点需要机器人去探险,有m条单向路径。问至少需要几个机器人才能遍历完所有的点,一个点可以被多个机器人经过(这就是和单纯的最小路径覆盖的区别)。
思路:这是个最小路径覆盖问题,但是因为有的点可以重复访问,所以最小路径是可以相交的,我们就用传递闭包建立新图(G’),转化为一般的路径覆盖,然后就是跟 poj1422 一样了。
   最小路径覆盖 = 图的顶点数 – 最大匹配数,所以只要用匈牙利算法求出最大匹配数,然后用顶点数一减就出来了。
AC代码:
 1 #include<vector>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<iostream>
 6 using namespace std;
 7 #define maxn 666
 8 int match[maxn];
 9 int vis[maxn];
10 int e[maxn][maxn];
11 int n,m;
12 int dfs(int u)
13     for(int i=1;i<=n;i++)
14         if(!vis[i]&&e[u][i])
15             vis[i]=1;
16             if(match[i]==0||dfs(match[i]))
17                 match[i]=u;
18                 return 1;
19             
20         
21     
22     return 0;
23 
24 void floyd()
25     for(int k=1;k<=n;k++)    
26         for(int i=1;i<=n;i++)
27             for(int j=1;j<=n;j++)
28                 if(e[i][k]&&e[k][j])
29                     e[i][j]=1;
30 
31 int main()
32         while(~scanf("%d%d",&n,&m)&&(n+m))
33             int x,y;
34             memset(match,0,sizeof(match));
35         
36             for(int i=1;i<=m;i++)
37                 scanf("%d%d",&x,&y);
38                 e[x][y]=1;
39             
40             floyd();
41             int ans=0;
42             for(int i=1;i<=n;i++)
43                 memset(vis,0,sizeof(vis));
44                 if(dfs(i))
45                     ans++;
46             
47             int res=n-ans;
48             printf("%d\n",res);
49             for(int i=0;i<=maxn;i++)
50                 for(int j=0;j<=maxn;j++)
51                     e[i][j]=0;
52         
53     return 0;
54 

 



以上是关于Treasure Exploration POJ - 2594 有向图路径可相交的最小路径覆盖模板题的主要内容,如果未能解决你的问题,请参考以下文章

POJ2594Treasure Exploration(最小路径覆盖,相交)

poj 2594 Treasure Exploration 二分图匹配

POJ2594 Treasure Exploration

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

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

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