POJ2594 Treasure Exploration【二分图最小路径覆盖】【Floyd】
来源:互联网 发布:udp源端口号 编辑:程序博客网 时间:2024/05/16 13:49
题目链接:
http://poj.org/problem?id=2594
题目大意:
给你N个地点,M条有向边,已知构成的图是有向无环图。现在要在地点上放机器人通过M
条边来遍历N个地点,问:最少需要多少个机器人可以遍历N个地点。
思路:
这是一道求最小路径覆盖的题目。和一般最小路径覆盖的题目不一样的地方是:这里的点可
以重复遍历。也就是可以有两个及以上的机器人经过同一个点。那么,先建立一个二分图,
两边都为N个地点。然后在原图的基础上,用Floyd求一次传递闭包,也就是如果点i可以到达
点j,而点j可以到达点k,那么可以当做点i可以直接跳过点j而到达点k,就可以建立一条有向
边(i,k)。建好图后,就是一般的二分图最小路径覆盖的问题了。而二分图最小路径覆盖 =
点数 - 二分图最大匹配,用匈牙利算法求出二分图最大匹配就可以了。
AC代码:
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespace std;const int MAXN = 550;bool Map[MAXN][MAXN],Mask[MAXN];int NX,NY;int cx[MAXN],cy[MAXN];void Floyd(int N) //求传递闭包{ for(int k = 1; k <= N; ++k) { for(int i = 1; i <= N; ++i) { for(int j = 1; j <= N; ++j) { if(i != j && Map[i][k] && Map[k][j]) Map[i][j] = 1; } } }}int FindPath(int u){ for(int i = 1; i <= NY; ++i) { if(Map[u][i] && !Mask[i]) { Mask[i] = 1; if(cy[i] == -1 || FindPath(cy[i])) { cy[i] = u; cx[u] = i; return 1; } } } return 0;}int MaxMatch() //二分图最大匹配{ for(int i = 1; i <= NX; ++i) cx[i] = -1; for(int i = 1; i <= NY; ++i) cy[i] = -1; int res = 0; for(int i = 1; i <= NX; ++i) { if(cx[i] == -1) { for(int j = 1; j <= NY; ++j) Mask[j] = 0; res += FindPath(i); } } return res;}int main(){ int N,M,u,v; while(~scanf("%d%d",&N,&M) && (N||M)) { memset(Map,0,sizeof(Map)); for(int i = 1; i <= M; ++i) { scanf("%d%d",&u,&v); Map[u][v] = 1; } Floyd(N); NX = NY = N; printf("%d\n",N - MaxMatch()); } return 0;}
0 0
- POJ2594 Treasure Exploration【二分图最小路径覆盖】【Floyd】
- POJ2594-Treasure Exploration(最小路径覆盖变形)
- POJ2594 Treasure Exploration(最小路径覆盖)
- poj2594 Treasure Exploration【最小路径覆盖】
- POJ2594 Treasure Exploration(二分图最小路径覆盖,传递闭包)
- 可重叠的最小路径覆盖 poj2594 Treasure Exploration
- poj2594 Treasure Exploration(可相交最小路径覆盖)
- (floyd+最小路径覆盖) Treasure Exploration (P2594)
- POJ 2594 Treasure Exploration(最小路径覆盖-hungary+Floyd)
- POJ 2594 Treasure Exploration (floyd判断联通+最小路径覆盖)
- poj 2594 Treasure Exploration (最小路径覆盖+Floyd缩点)
- poj2594 Treasure Exploration(二分图)
- poj2594 Treasure Exploration 最小路径覆盖=顶点数-最大匹配数
- poj2594——Treasure Exploration(闭包+最小路径覆盖)
- poj2594-Treasure Exploration(最小路径覆盖,最大匹配,floyed(优化))
- poj 2594 Treasure Exploration 可重复覆盖的二分图最小覆盖路径
- POJ 2594 Treasure Exploration-有向图最小路径覆盖&Floyd
- POJ2594 Treasure Exploration 最小点覆盖题目[有重复点]
- unix-ipc
- C++插件架构浅谈与初步实现
- HDU 2612 Find a way (bfs)
- 超棒的Web和移动应用开发框架(三)
- from nova to ironic(5)
- POJ2594 Treasure Exploration【二分图最小路径覆盖】【Floyd】
- Android如何使用JDBC连接SQLite数据库?
- UNIX练习-tcp/ip客户端( - - 写unix得审核....)
- 最短路模板 Dijkstra+Floyd+SPFA
- Quartz-Spring[一]之MethodInvokingJobDetailFactoryBean配置任务
- 安卓实习期间整理知识点(五)
- Java异常
- Android:资源 id 及资源 id 的动态获取
- unix - tcp/ip服务端..