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

传送门: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

POJ Monthly--2005.08.28,Li Haoyuan

题意概括:

给一个 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 }

原文地址:https://www.cnblogs.com/ymzjj/p/9991880.html

时间: 2024-10-13 03:54:30

POJ Treasure Exploration 【DAG交叉最小路径覆盖】的相关文章

POJ 2594 Treasure Exploration【传递闭包+最小路径覆盖】

大意: 有n个点,告诉你一些单向边,问多少条边能把所有的点覆盖[注意点能重复覆盖  比如4->1->2   5->3] 分析: 知识储备: 传递闭包:  所谓传递,可以这么理解,对于节点j如果i能到k并且k能到j那么i能到j,这样用像floyed就能处理出任意两个点能否到达 for(int k = 1; k <= n; k++) { for(int i = 1; i <= n; i++) { if(W[i][k]) { for(int j = 1; j <= n; j+

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

Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 9794   Accepted: 3975 题目链接:http://poj.org/problem?id=2594 Description: Have you ever read any book about treasure exploration? Have you ever see any film about treasure

POJ 2594--Treasure Exploration【二分图 &amp;&amp; 最小路径覆盖 &amp;&amp; 点可以重复走 &amp;&amp; 传递闭包】

Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7343   Accepted: 3002 Description Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploration? Have you ever explored

POJ2594 Treasure Exploration【二分图最小路径覆盖】【Floyd】

题目链接: http://poj.org/problem?id=2594 题目大意: 给你N个地点,M条有向边,已知构成的图是有向无环图.现在要在地点上放机器人通过M 条边来遍历N个地点,问:最少需要多少个机器人可以遍历N个地点. 思路: 这是一道求最小路径覆盖的题目.和一般最小路径覆盖的题目不一样的地方是:这里的点可 以重复遍历.也就是可以有两个及以上的机器人经过同一个点. 那么,先建立一个二分图, 两边都为N个地点.然后在原图的基础上,用Floyd求一次传递闭包,也就是如果点i可以到达 点j

UVAlive3126 Taxi Cab Scheme(DAG的最小路径覆盖)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32568 [思路] DAG的最小路径覆盖. 将每个人看做一个结点,如果时间允许到达就连边,则问题转化为DAG上的最小路径覆盖问题,即找到最少的路径使得每个点位于一条路径上. 算法:将DAG中的每个结点u拆分成2个为u1,u2,如果DAG中有边uv则连边u1-v2.如果该二分图的最大匹配数为ans,则答案为n-ans.可以这样想:在一条路径中除尾结点外其他结点都有且仅

exam1802 Bounty Hunter II(DAG的最小路径覆盖)

原文:http://www.cnblogs.com/jackiesteed/articles/2043934.html DAG的最小路径覆盖是指找最小数目的互相不相交的有向路径,满足DAG的所有顶点都被覆盖. 首先给出公式:DAG的最小路径覆盖数=DAG图中的节点数-相应二分图中的最大匹配数. 那么对应一个DAG,如何构造相应的二分图?对于DAG中的一个顶点p,二分图中有两个顶点p和p',对应DAG中的一条有向边p->q,二分图中有p-q'的一条无向边.二分图中p属于S集合,p'属于T集合. 下

POJ 2594 Treasure Exploration 最小可相交路径覆盖

最小路径覆盖 DAG的最小可相交路径覆盖: 算法:先用floyd求出原图的传递闭包,即如果a到b有路径,那么就加边a->b.然后就转化成了最小不相交路径覆盖问题. 这里解释一下floyd的作用如果1->2->3->4那么1可以到达2,3,4只要需要借助一些点,那么就可以直接把1与2,3,4相连,这就是floyd要做的事. 证明:为了连通两个点,某条路径可能经过其它路径的中间点.比如1->3->4,2->4->5.但是如果两个点a和b是连通的,只不过中间需要经

poj 1422 Air Raid (最小路径覆盖)

链接:poj 1422 题意:有n个点和m条有向边,现在要在点上放一些伞兵,伞兵可以沿着图走, 直到不能走为止,每条边有且仅有一个伞兵走过,问最少放多少个伞兵 思路:求的最小路径覆盖,用二分图来做 对于这样的一个有向图做最小路径覆盖,首先建图 然后每一条有向边对应左边的点指向右边的点 这样建好图之后求最大匹配数 最小路径覆盖=点数-最大匹配数 [cpp] view plaincopyprint? #include<stdio.h> #include<string.h> int n,

POJ - 3020 Antenna Placement 二分图 最小路径覆盖

题目大意:有n个城市,要在这n个城市上建立无线电站,每个无线电站只能覆盖2个相邻的城市,问至少需要建多少个无线电站 解题思路:英语题目好坑,看了半天.. 这题和POJ - 2446 Chessboard类似 可以将所有城市分成两个点集,那么之间的连线就代表无线电站的覆盖关系了. 因为所有城市都要覆盖到,所以根据关系,求出最小路径覆盖就能覆盖所有城市了 #include<cstdio> #include<algorithm> #include<cstring> #incl