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

本文探讨了一个关于机器人在火星上探索未知地点的算法问题,通过构建有向图并使用Floyd算法求解传递闭包,最终实现用最少数量的机器人覆盖所有点的目标。

传送门: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 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跑一遍求出原图的传递闭包,再跑最小路径覆盖就能解决上面的问题了。

 

AC code:

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <cmath>
 6 #define INF 0x3f3f3f3f
 7 using namespace std;
 8 const int MAXN = 510;
 9 int g[MAXN][MAXN];
10 int linker[MAXN];
11 bool used[MAXN];
12 int N, M;
13 
14 bool Find(int x)
15 {
16     for(int i = 1; i <= N; i++){
17         if(!used[i] && g[x][i]){
18             used[i] = true;
19             if(linker[i] == -1 || Find(linker[i])){
20                 linker[i] = x;
21                 return true;
22             }
23         }
24     }
25     return false;
26 }
27 void Floyd()
28 {
29     for(int i = 1; i <= N; i++){
30         for(int j = 1; j <= N; j++){
31             for(int k = 1; k <= N; k++){
32                 if(g[i][k] == 1 && g[k][j] == 1) g[i][j] = 1;
33             }
34         }
35     }
36 }
37 
38 void init()
39 {
40     memset(g, 0,sizeof(g));
41     memset(linker, -1, sizeof(linker));
42 }
43 int main()
44 {
45     int u, v;
46     while(~scanf("%d%d", &N, &M) && (N+M)){
47         init();
48         while(M--){
49             scanf("%d%d", &u, &v);
50             g[u][v] = 1;
51         }
52         Floyd();
53         int ans = 0;
54         for(int i = 1; i <= N; i++){
55             memset(used, 0, sizeof(used));
56             if(Find(i)) ans++;
57         }
58         printf("%d\n", N-ans);
59     }
60     return 0;
61 }
View Code

 

转载于:https://www.cnblogs.com/ymzjj/p/9991880.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值