POJ--2594|Treasure Exploration

来源:互联网 发布:浏览器如何打开php文件 编辑:程序博客网 时间:2024/05/17 23:57
Treasure Exploration
Time Limit: 6000MS Memory Limit: 65536KTotal Submissions: 8223 Accepted: 3371

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 02 11 22 00 0

Sample Output

112

Source

POJ Monthly--2005.08.28,Li Haoyuan




刚刚接触这题,觉得一个Floyd可以搞定,但是WA了几次后发现,可能还是题目的意思没有读透。

有一些点,从某个点出发走下去,要走完所有点。问需要多少个机器人。题目已经说了可以重点以及是有向图。

这是个求最小独立集的过程,顾名思义就是把所有点连起来的最小路径(也叫最小路径覆盖)。

有公式:最小独立集=所有顶点数-最大匹配;

终于还是在第6次AC了这道题。人老了...

/*name:Rollchuchytype:*/#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>using namespace std;vector<int> v[505];int dis[505][505];int match[505];bool vis[505];const int INF=1e9+7;int n,m;void clear(){for(int i=1;i<=500;i++){v[i].clear();for(int j=1;j<=500;j++){dis[i][j]=0;}}}int find(int u){for(int i=0;i<v[u].size();i++){if(!vis[v[u][i]]){vis[v[u][i]]=1;if(match[v[u][i]]==-1||find(match[v[u][i]])){match[v[u][i]]=u;return 1;}}}return 0;}int solve(){memset(match,-1,sizeof(match));int ans=0;for(int i=1;i<=n;i++){memset(vis,0,sizeof(vis));ans+=find(i);}return ans;}int main(){//freopen("in.txt","r",stdin);while(~scanf("%d %d",&n,&m)){clear();if(n==0&&m==0)break;int a,b;for(int i=0;i<m;i++){scanf("%d %d",&a,&b);dis[a][b]=1;}for(int k=1;k<=n;k++){for(int i=1;i<=n;i++){if(dis[i][k])for(int j=1;j<=n;j++){dis[i][j]=(dis[i][j]||(dis[i][k]&&dis[k][j]));}}}for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){if(dis[i][j]){v[i].push_back(j);}}}cout<<n-solve()<<endl; }return 0; } 


0 0
原创粉丝点击