poj 2594 Treasure Exploration 【warshall 求传递闭包 + 最小路径覆盖】
来源:互联网 发布:知乎 成都留学机构 编辑:程序博客网 时间:2024/05/17 00:07
Treasure Exploration
Time Limit: 6000MS Memory Limit: 65536KTotal Submissions: 7201 Accepted: 2938
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?
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 02 11 22 00 0
Sample Output
112
题意:给出一个n个点,m条边的有向图。一个机器人可以从一个端点,一直走到前方没有路为止(不能返回 继续走),问你最少需要派多少机器人才能访问完所有的点。
注意:一个点可以被多次访问,这和最小路径覆盖不相符,为了实现一个点可以重复访问,我们可以用Warshall算法求出传递闭包构建新图,然后再求出最小路径覆盖。
AC代码:1786ms 邻接矩阵
#include <cstdio>#include <cstring>#include <vector>#include <algorithm>#define MAXN 500+10using namespace std;int Map[MAXN][MAXN];int pipei[MAXN], used[MAXN];int n, m;void getMap(){int a, b;while(m--){scanf("%d%d", &a, &b);Map[a][b] = 1;}}void war_shall(){for(int k = 1; k <= n; k++){for(int i = 1; i <= n; i++){for(int j = 1; j <= n; j++)Map[i][j] = Map[i][k] & Map[k][j] || Map[i][j];} }}int find(int x){for(int i = 1; i <= n; i++){if(!used[i] && Map[x][i]){used[i] = 1;if(pipei[i] == -1 || find(pipei[i])){pipei[i] = x;return 1;}}} return 0;} void solve(){int ans = 0;memset(pipei, -1, sizeof(pipei));for(int i = 1; i <= n; i++){memset(used, 0, sizeof(used));ans += find(i); } printf("%d\n", n- ans);//最小路径覆盖 }int main(){while(scanf("%d%d", &n, &m), n||m){memset(Map, 0, sizeof(Map));//初始化 getMap();//得到原图 war_shall();//求传递闭包 构建新图 solve();} return 0;}
AC代码:邻接表 1110ms
#include <cstdio>#include <cstring>#include <vector>#include <algorithm>#define MAXN 500+10#define MAXM 250000+100using namespace std;struct Edge{int to, next;}edge[MAXM];int head[MAXN], edgenum;int Map[MAXN][MAXN];int pipei[MAXN], used[MAXN];int n, m;void init(){edgenum = 0;memset(head, -1, sizeof(head));}void addEdge(int u, int v){Edge E = {v, head[u]};edge[edgenum] = E;head[u] = edgenum++;} void getMap(){int a, b;while(m--){scanf("%d%d", &a, &b);Map[a][b] = 1;addEdge(a, b); }}void war_shall(){for(int k = 1; k <= n; k++){for(int i = 1; i <= n; i++){for(int j = 1; j <= n; j++){if(Map[i][k] && Map[k][j] && !Map[i][j])Map[i][j] = 1, addEdge(i, j);}} }}int find(int x){for(int i = head[x]; i != -1; i = edge[i].next){int v = edge[i].to;if(!used[v]){used[v] = 1;if(pipei[v] == -1 || find(pipei[v])){pipei[v] = x;return 1;}}} return 0;} void solve(){int ans = 0;memset(pipei, -1, sizeof(pipei));for(int i = 1; i <= n; i++){memset(used, 0, sizeof(used));ans += find(i); } printf("%d\n", n- ans);//最小路径覆盖 }int main(){while(scanf("%d%d", &n, &m), n||m){memset(Map, 0, sizeof(Map));//初始化 init();getMap();//得到原图 war_shall();//求传递闭包 构建新图 solve();} return 0;}
0 0
- poj 2594 Treasure Exploration 【warshall 求传递闭包 + 最小路径覆盖】
- POJ Treasure Exploration 2594【Warshall求传递闭包+最小路径覆盖】
- POJ 2594 Treasure Exploration(传递闭包+最小路径覆盖)
- poj 2594 Treasure Exploration最小路径覆盖+传递闭包
- POJ 2594 Treasure Exploration(最小路径覆盖+Floyd求传递闭包)
- poj 2594 Treasure Exploration(可重点最小路径覆盖)(二分匹配+Floyd求传递闭包)
- POJ-2594-Treasure Exploration-(路径相交的最小路径覆盖+闭包传递)
- POJ 2594--Treasure Exploration【二分图 && 最小路径覆盖 && 点可以重复走 && 传递闭包】
- POJ-2594 Treasure Exploration floyd传递闭包+最小路径覆盖,nice!
- POJ 2594 Treasure Exploration (最小路径覆盖+传递闭包(解决可重点))
- POJ 2594 Treasure Exploration (最小路径覆盖+传递闭包(解决可重点))+传递闭包详解
- POJ 2594 Treasure Exploration 最小路径覆盖
- poj 2594 Treasure Exploration 最少边覆盖+传递闭包
- POJ2594 Treasure Exploration(二分图最小路径覆盖,传递闭包)
- Treasure Exploration 【传递闭包】+【最小顶点覆盖】
- POJ 2594 Treasure Exploration(最小路径覆盖变形)
- POJ 题目2594 Treasure Exploration(最小路径覆盖)
- poj 2594 Treasure Exploration 最小路径覆盖/最大匹配
- 导入 Android v4 包看不到源码的解决办法
- [转]如何测试网页的登录页面
- 动态加载APK原理分享
- 4.根据下面的主类,编写一个立方体类Cube,该类中有求矩形面积area()和体积volume()的方法,而且每种方法都有一个无参的和有参的方法构成过载。
- [C++设计模式] state 状态模式
- poj 2594 Treasure Exploration 【warshall 求传递闭包 + 最小路径覆盖】
- Android招财进宝手势密码的实现
- JAVA的三大变量分析
- Intel平台下Linux中ELF文件动态链接的加载、解析及实例分析
- 一个之前没注意的小知识点1
- flash 调用启动一个.exe文件并向其传递参数的方法
- How to implement growable memory-mapped files (GMMFs) in NTFS
- IPTABLES进阶
- python中字符串的使用