POJ - 2594 Treasure Exploration(floyd传递闭包+最小路径覆盖)

Treasure Exploration

Time Limit: 6000MS   Memory Limit: 65536K
Total Submissions: 9490   Accepted: 3871

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

       这道题首先就是想到了之前做过的二分图的最小路径问题,交了一发过后发现wa了;仔细读题后发现这样一个问题;

       之前的标准的路径覆盖问题,他所要求的一点是要所有的路径不能交叉,即从所有路径远点到终点后,经过每个点的次数有且仅有1而已。

      而这道题,他是允许多次到达某一个点的,这个时候我们就需要用 floyd!传递闭包!了,即假如边有相交,那么他们的连通性也应该连通下去。

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int G[505][505];
int vis[505];
int use[505];
int n, m;
int find(int x) {
	for (int s = 1; s <= n; s++) {
		if (!vis[s] && G[x][s]) {
			vis[s] = 1;
			if (use[s] == 0 || find(use[s])) {
				use[s] = x;
				return  1;
			}
		}
	}
	return 0;
}
void floyd()
{
	for (int s = 1; s <= n; s++) 
		for (int w = 1; w <= n; w++) 
			for (int e = 1; e <= n; e++) 
				if (G[e][s] && G[s][w])
					G[e][w] = 1;
}
int main()
{
	ios::sync_with_stdio(0);
	while (cin >> n >> m&&n+m) {
		memset(G, 0, sizeof(G));
		memset(use, 0, sizeof(use));
		while (m--) {
			int a, b; cin >> a >> b;
			G[a][b] = 1;
		}
		floyd();
		int ans = 0;
		for (int s = 1; s <= n; s++) {
			memset(vis, 0, sizeof(vis));
			if (find(s))ans++;
		}
		cout << n - ans << endl;
	}
	return 0;
}

 

全部评论

相关推荐

菜菜咪:1. 可以使用简历网站的模版,美观度会更好一点 2. 邮箱可以重新申请一个,或者用qq邮箱的别名,部分hr可能会不喜欢数字邮箱 3. 项目经历最好分点描述,类似的项目很多,可以参考一下别人怎么写的 4. 自我评价可加可不加,技术岗更看重技术。最后,加油,优秀士兵
点赞 评论 收藏
分享
10-18 13:01
已编辑
西安理工大学 C++
小米内推大使:建议技能还是放上面吧,hr和技术面试官第一眼想看的应该是技能点和他们岗位是否匹配
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务