http://www.elijahqi.win/2017/12/15/poj2594treasure-exploration/
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
这题让我想到了leoly的接站问题
应该也是同样的套路吧
首先floyd传递闭包把可相交最少路径覆盖问题转化为不可相交的最少路径覆盖问题
floyd把那些边互相可达提前预处理出来 然后连边
然后跑一遍dinic即可
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 1100
#define inf 0x3f3f3f3f
using namespace std;
inline char gc(){
static char now[1<<16],*S,*T;
if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0;char ch=gc();
while(ch<'0'||ch>'9') ch=gc();
while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();}
return x;
}
struct node{
int y,z,next;
}data[N*N];
int T,n,m,mp[N][N],h[N],level[N],num;
inline void insert1(int x,int y,int z){
data[++num].y=y;data[num].z=z;data[num].next=h[x];h[x]=num;
data[++num].y=x;data[num].z=0;data[num].next=h[y];h[y]=num;
}
inline bool bfs(){
memset(level,0,sizeof(level));queue<int>q;level[0]=1;q.push(0);
while(!q.empty()){
int x=q.front();q.pop();
for (int i=h[x];i;i=data[i].next){
int y=data[i].y,z=data[i].z;
if (level[y]||!z) continue;level[y]=level[x]+1;q.push(y);if(y==T) return 1;
}
}return 0;
}
inline int dfs(int x,int s){
if (x==T) return s;int ss=s;
for (int i=h[x];i;i=data[i].next){
int y=data[i].y,z=data[i].z;
if(level[x]+1==level[y]&&z){
int xx=dfs(y,min(z,s));if (!xx) level[y]=0;
s-=xx;data[i].z-=xx;data[i^1].z+=xx;if (!s) return ss;
}
}return ss-s;
}
int main(){
freopen("poj2594.in","r",stdin);
while(1){
n=read();m=read();if (!n&&!m) break;
num=1;memset(mp,0,sizeof(mp));T=n+n+1;memset(h,0,sizeof(h));
for (int i=1;i<=m;++i){int x=read(),y=read();mp[x][y]=1;}
for (int k=1;k<=n;++k)
for (int i=1;i<=n;++i)
for (int j=1;j<=n;++j)
mp[i][j]|=mp[i][k]&mp[k][j];
for (int i=1;i<=n;++i) insert1(0,i,1);
for (int i=1;i<=n;++i)
for(int j=1;j<=n;++j) if (i!=j) if (mp[i][j]) insert1(i,j+500,1);
for (int i=1;i<=n;++i) insert1(i+500,T,1);
int ans=0;while(bfs()) ans+=dfs(0,inf);printf("%d\n",n-ans);
}
return 0;
}