POJ 2594 —— Treasure Exploration——————【最小路径覆盖、可重点、floyd传递闭包】

时间:2021-08-14 17:23:54
Treasure Exploration

Time Limit:6000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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
题目大意:多组数据。每组给n,m。表示有n个点,m条有向边。让机器人访问n个点,每个机器人不能走回头路,但是不同的机器人可以走其他机器人走过的点。问你最少需要多少个机器人。
解题思路:最小路径覆盖。首先用floyd传递闭包。然后再跑最大匹配。
样例:   5 4
    1 2
    3 2
    2 4
    2 5
按照一般的最小路径覆盖,那么需要3个机器人,但是经过floyd传递闭包后,其实只需要2个即可。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 600;
const int INF = 0x3f3f3f3f;
vector<int>G[maxn];
int Mx[maxn], My[maxn], dx[maxn], dy[maxn], used[maxn], dis;
int Map[maxn][maxn];
bool SearchP(int _n){
queue<int>Q;
memset(dx,-1,sizeof(dx));
memset(dy,-1,sizeof(dy));
int dis = INF;
for(int i = 1; i <= _n; i++){
if(Mx[i] == -1){
dx[i] = 0;
Q.push(i);
}
}
int v;
while(!Q.empty()){
int u = Q.front(); Q.pop();
if(dx[u] > dis) break;
for(int i = 0; i < G[u].size(); i++){
v = G[u][i];
if(dy[v] == -1){
dy[v] = dx[u] + 1;
if(My[v] == -1){
dis = dy[v];
}else{
dx[My[v]] = dy[v] + 1;
Q.push(My[v]);
}
}
}
}
return dis != INF;
}
int dfs(int u){
int v;
for(int i = 0; i < G[u].size(); i++){
v = G[u][i];
if(!used[v] && dy[v] == dx[u] + 1){
used[v] = 1;
if(My[v] != -1 && dy[v] == dis){
continue;
}
if(My[v] == -1 || dfs(My[v])){
Mx[u] = v;
My[v] = u;
return true;
}
}
}
return false;
}
int MaxMatch(int ln,int rn){
int ret = 0;
memset(Mx,-1,sizeof(Mx));
memset(My,-1,sizeof(My));
while(SearchP(ln)){
memset(used,0,sizeof(used));
for(int i = 1; i <= ln; i++){
if(Mx[i] == -1 && dfs(i)){
ret++;
}
}
}
return ret;
}
int main(){
int T, cas = 0, n, m, N, M, k;
while(scanf("%d%d",&N,&M)!=EOF&&(N+M)){
for(int i = 0; i <= N; i++){
G[i].clear();
}
int a,b;
memset(Map,0,sizeof(Map));
for(int i = 1; i <= M; i++){
scanf("%d%d",&a,&b);
Map[a][b] = 1;
}
for(int k = 1; k <= N; k++){
for(int i = 1; i <= N; i++){
for(int j = 1; j <= N; j++){
Map[i][j] = Map[i][j]|| Map[i][k]&&Map[k][j];
}
}
} for(int i = 1; i <= N; i++){
for(int j = 1; j <= N; j++){
if(Map[i][j]){
G[i].push_back(j);
}
}
}
n = m = N;
int res = MaxMatch(n,m);
printf("%d\n",N-res);
}
return 0;
}