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;
}

 

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-07 14:00
不想多说了,什么逆天HR,还要教我礼貌😂
机械打工仔:这不纯傻卵吗,他还操心上别人老板了
投递BOSS直聘等公司7个岗位
点赞 评论 收藏
分享
深夜书店vv:腾讯是这样的,去年很多走廊都加桌子当工区
点赞 评论 收藏
分享
06-15 20:57
已编辑
门头沟学院 Java
CARLJOSEPH...:年轻人有傲气很正常,但是建议工作前洗净傲气。 说实在的,什么奖学金什么奖项的都很一般。尊重你的老师,在有时间的时候去上课,真遇到走不开的事,请态度端正地向你的老师说明情况,请求请假。我相信任何一个有师德的老师都会允许的(我的老师就是这样)。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务