poj2594
来源:互联网 发布:linux vnc服务器 编辑:程序博客网 时间:2024/05/05 05:34
D - Treasure Exploration
Time Limit:6000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64uDescription
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
题意:在火星上有一些宝藏,派一些机器人去发掘宝藏,每个机器人可以单向从火星上的一些点去往其他点,不同机器人可以走相同点,希望用最少的机器人去吧所有宝藏发掘出来;
这是一道最小路径覆盖问题,但是终点可以重复利用,最小路径覆盖=点数-最大匹配,在这道题中,因为需要重复的地方只要飞过去,就可以不重复了。赋予这个能力的方法就是把所有点能间接到达的点全都改为直接到达。然后正常求最小路径覆盖即可。
ac代码:
#include <iostream>#include <stdio.h>#include <vector>#include <string.h>#include <queue>using namespace std;const int maxn=505;vector<int>G[maxn];int map[maxn][maxn];int used[maxn],linker[maxn];int N;bool dfs(int u){ for(int i=1;i<=N;i++) { if(!used[i]&&map[u][i]) { used[i]=1; if(linker[i]==-1||dfs(linker[i])) { linker[i]=u; return true; } } } return false;}int solve(int n){ int res=0; memset(linker,-1,sizeof(linker)); for(int u=1;u<=n;u++) { memset(used,0,sizeof(used)); if(dfs(u))res++; } return res;}int main(){ int n,m; while(scanf("%d%d",&n,&m)!=-1) { N=n; if(n==0&&m==0)break; int u,v; memset(map,0,sizeof(map)); for(int i=0; i<m; i++) { scanf("%d%d",&u,&v); G[u].push_back(v); map[u][v]=1; } for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) for(int k=1;k<=n;k++) if(k!=j&&map[j][i]&&map[i][k]) map[j][k]=1; int ans=n-solve(n); if(ans==0) ans=1; cout<<ans<<endl; for(int i=1; i<=n; i++) G[i].clear(); } return 0;}
0 0
- poj2594
- POJ2594
- poj2594 Treasure Exploration
- POJ2594 最小路径覆盖
- POJ2594-Treasure Exploration
- poj2594:Treasure Exploration
- poj2594 treasure exploration
- POJ2594-Treasure Exploration
- 最小路径覆盖 POJ2594 POJ1422
- 【POJ2594】Treasure Exploration 宝藏勘探
- POJ2594拐点弯的二分
- poj2594(DAG最小路径覆盖)
- poj2594(最小路径覆盖+floyd)
- POJ2594-Treasure Exploration(最小路径覆盖变形)
- poj2594 (最小路径覆盖 + floyd)
- poj2594——最小路径覆盖
- poj2594(最小路径覆盖 + floyd)
- poj2594(可相交最小路径覆盖)
- android RSA加密的使用
- NSAssert()
- 实现单张图片的倒影效果
- 343. Integer Break
- FileReader搭配FileWriter,更新对抛出的机制做回应
- poj2594
- 基于JavaScript代码去掉H5页面中的头尾及广告部分(支持 Android 和 iOS)
- Unity3D的几种坐标系,以及屏幕解锁类似功能
- hdu1978——How many ways(记忆化dp)
- apache commons fileupload 问题跟踪系统
- js url上添加随机数防止缓存
- 结构体写法用于set map暂时乱存一下
- PCM音频文件的制作
- 佛主保佑,永无BUG