【POJ - 2594】Treasure Exploration(floyd传递闭包 + 最小路径覆盖,图论)

题干:

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

题目大意:

给一张n个点的有向图,问最少放几个机器人能走完整个图,机器人可以任意选一个点放置,一个点可以被多个机器人经过(例如1->3,2->3,3->4,3->5.这种情况需要两个机器人),保证图中没有环, 不保证图连通。

解题报告:

因为我们要选择最少的路径来覆盖所有的点,那么显然这是一个最小路径覆盖问题。但是这题有个问题就在于一个点可以被多个机器人经过(这就是和单纯的最小路径覆盖的区别)。如果我们用裸的最小路径覆盖,那么“题目大意”里给的那个样例应该输出3,但是其实应该输出2,这也就是说我们应该让每一个点可以到达的点全都置为true,然后再跑二分图,这样得到的才是正解、

做法就是求个floyd传递闭包,其实邻接表建图然后用Dijkstra也可以,好像这样更快?反正floyd是800ms才水过。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 555 + 5;
bool f[MAX][MAX];
int n,m;
void floyd() {
	for(int k = 1; k<=n; k++) {
		for(int i = 1; i<=n; i++) {
			for(int j = 1; j<=n; j++) {
				if(f[i][k] && f[k][j]) f[i][j] = 1;
 			}
		}
	}	
}
bool used[MAX];
int nxt[MAX];
bool find(int x) {
	for(int i = 1; i<=n; i++) {
		if(used[i] == 0 && f[x][i]) {
			used[i] = 1;
			if(nxt[i] == 0|| find(nxt[i])) {
				nxt[i] = x;
				return 1;
			} 
		}
	}
	return 0 ;
}
int match() {
	int res = 0;
	for(int i = 1; i<=n; i++) {
		memset(used,0,sizeof used);
		if(find(i)) res++;
	}
	return res;
}
int main()
{
	while(~scanf("%d%d",&n,&m) && n+m) {
		memset(f,0,sizeof f);
		memset(nxt,0,sizeof nxt);
		for(int a,b,i = 1; i<=m; i++) {
			scanf("%d%d",&a,&b);
			f[a][b]=1;
		}
		floyd();
		printf("%d\n",n - match());
	}


	return 0 ;
}

 

总结:

这题可以用强连通分量吗?

这题说了没有环,也就是没有强连通分量啊,tarjan没用啊。。。

能不能用树形dp?

说了没有环不代表他就是树,比如这个样例:

8 8
1 3
2 3 
3 4 
3 5 
4 6 
5 6 
6 7 
6 8 
最终应该输出2

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值