POJ2594 Treasure Exploration

Treasure Exploration

Time Limit: 6000MS   Memory Limit: 65536K
Total Submissions: 8879   Accepted: 3635

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

【题解】

二分图可重最小路径覆盖裸题

先求传递闭包,然后连边

注意时间,数组尽量开小

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5
 6 inline void read(int &x)
 7 {
 8     x = 0;char ch = getchar(), c = ch;
 9     while(ch < ‘0‘ || ch > ‘9‘)c = ch, ch = getchar();
10     while(ch <= ‘9‘ && ch >= ‘0‘)x = x * 10 + ch - ‘0‘, ch = getchar();
11     if(c == ‘-‘)x = -x;
12 }
13
14 const int INF = 0x3f3f3f3f;
15
16 struct Edge
17 {
18     int u,v,next;
19     Edge(int _u, int _v, int _next){u = _u, v = _v, next = _next;}
20     Edge(){}
21 }edge[250010];
22
23 int head[510], cnt;
24
25 inline void insert(int a, int b)
26 {
27     edge[++cnt] = Edge(a,b,head[a]);
28     head[a] = cnt;
29 }
30
31 int n,m,lk[510],b[510],g[510][610];
32
33 int dfs(int u)
34 {
35     for(register int pos = head[u];pos;pos = edge[pos].next)
36     {
37         int v = edge[pos].v;
38         if(b[v])continue;
39         b[v] = 1;
40         if(lk[v] == -1 || dfs(lk[v]))
41         {
42             lk[v] = u;
43             return 1;
44         }
45     }
46     return 0;
47 }
48
49 int xiongyali()
50 {
51     int ans = 0;
52     memset(lk, -1, sizeof(lk));
53     for(register int i = 1;i <= n;++ i)
54     {
55         memset(b, 0, sizeof(b));
56         ans += dfs(i);
57     }
58     return ans;
59 }
60
61 int main()
62 {
63     while(scanf("%d %d", &n, &m) != EOF && (n + m))
64     {
65         memset(edge, 0, sizeof(edge));
66         memset(head, 0, sizeof(head));
67         memset(g, 0, sizeof(g));
68         cnt = 0;
69         int tmp1, tmp2;
70         for(register int i = 1;i <= m;++ i)
71         {
72             read(tmp1), read(tmp2);
73             g[tmp1][tmp2] = 1;
74         }
75         for(register int k = 1;k <= n;++ k)
76             for(int i = 1;i <= n;++ i)
77                 for(int j = 1;j <= n;++j)
78                     g[i][j] |= g[i][k] && g[k][j];
79         for(register int i = 1;i <= n;++ i)
80             for(int j = 1;j <= n;++ j)
81                 if(g[i][j]) insert(i, j);
82         printf("%d\n", n - xiongyali());
83     }
84     return 0;
85 } 

POJ2594

时间: 2024-11-05 16:38:36

POJ2594 Treasure Exploration的相关文章

POJ2594 Treasure Exploration[DAG的最小可相交路径覆盖]

Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 8301   Accepted: 3402 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

POJ2594 Treasure Exploration(最小路径覆盖+传递闭包)

题意: 派机器人去火星寻宝,给出一个无环的有向图,机器人可以降落在任何一个点上, 再沿着路去其他点探索,我们的任务是计算至少派多少机器人就可以访问到所有的点.点可以重复去. 思路: 最小路径覆盖,只是点可以重复去,就需要求传递闭包,用floyd /* *********************************************** Author :devil Created Time :2016/5/17 16:45:13 *****************************

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 二分图匹配

点击打开链接题目链接 Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7215   Accepted: 2947 Description Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploration? Have you eve

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

POJ 2594 Treasure Exploration 题目链接 题意:有向无环图,求最少多少条路径能够覆盖整个图,点能够反复走 思路:和普通的最小路径覆盖不同的是,点能够反复走,那么事实上仅仅要在多一步.利用floyd求出传递闭包.然后依据这个新的图去做最小路径覆盖就可以 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using names

poj 2594 Treasure Exploration (二分匹配)

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

POJ 2594 —— Treasure Exploration——————【最小路径覆盖、可重点、floyd传递闭包】

Treasure Exploration Time Limit:6000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2594 Description Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploratio

Poj 2594 Treasure Exploration (最小边覆盖+传递闭包)

题目链接: Poj 2594 Treasure Exploration 题目描述: 在外星上有n个点需要机器人去探险,有m条单向路径.问至少需要几个机器人才能遍历完所有的点,一个点可以被多个机器人经过. 解题思路: 一眼看上去是普通的最小边覆盖,但是仔细想后发现如果在原图上进行最大匹配的话,每个点只能经过一次.这样的话对于本题求出的并不是最优解,所以我们要先对原图进行传递闭包处理,然后再进行最大匹配. 这个题目点数太少H_K和匈牙利算法在空间和时间上并没有什么差,就代码复杂度而言匈牙利算法更有优