POJ2594 Treasure Exploration【DAG有向图可相交的最小路径覆盖】

题目链接:http://poj.org/problem?id=2594

Treasure Exploration
Time Limit: 6000MS   Memory Limit: 65536K
Total Submissions:10480   Accepted: 4250

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.
题目大意:给出一张有向图,求最小多少个机器人可以经过所有的点。
思路:
1.因为是可重复经过点的,所以路径是可相交的。求最小路径覆盖 = 原图顶点数 - 新图最大匹配数。
2.对于可相交的最小路径覆盖问题,要先用 floyd 求一下传递闭包。转化为不可相交的最小路径覆盖来求。
代码如下:
 1 #include<stdio.h>
 2 #include<string.h>
 3 #define mem(a, b) memset(a, b, sizeof(a))
 4 const int MAXN = 550;
 5 const int MAXM = 5100;
 6 
 7 int n, m; //n个点 m条有向边 
 8 int line[MAXN][MAXN], used[MAXN], master[MAXN];
 9 
10 void floyd()
11 {
12     for(int i = 1; i <= n; i ++)
13         for(int j = 1; j <= n; j ++)
14             for(int k = 1; k <= n; k ++)
15                 if(line[i][k] && line[k][j])
16                     line[i][j] = 1;
17 }
18 
19 int find(int x)
20 {
21     for(int i = 1; i <= n; i ++) //y部的点 
22     {
23         if(line[x][i] && used[i] == -1)
24         {
25             used[i] = 1;
26             if(master[i] == -1 || find(master[i]))
27             {
28                 master[i] = x;
29                 return 1;
30             }
31         }
32     }
33     return 0;
34 }
35 
36 int main()
37 {
38     while(scanf("%d%d", &n, &m) != EOF)
39     {
40         if(n == 0 && m == 0)
41             break;
42         int cnt = 0;
43         mem(line, 0), mem(master, -1);
44         for(int i = 1; i <= m; i ++)
45         {
46             int a, b;
47             scanf("%d%d", &a, &b);
48             line[a][b] = 1;
49         }
50         floyd();
51         for(int i = 1; i <= n; i ++) //x部的点 
52         {
53             mem(used, -1);
54             if(find(i))  
55                 cnt ++; //最大匹配数 
56         }
57         printf("%d\n", n - cnt);
58     }
59     return 0;
60 }
View Code

猜你喜欢

转载自www.cnblogs.com/yuanweidao/p/11481946.html