POJ 2594 Treasure Exploration【最小路径覆盖+缩点】

来源:互联网 发布:乳胶漆配色软件. 编辑:程序博客网 时间:2024/06/07 00:29

Treasure Exploration
Time Limit: 6000MS Memory Limit: 65536KTotal Submissions: 7228 Accepted: 2954

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


题目链接。



题目大意:
给出火星藏宝图,图中有宝藏地点,且各点之间的道路是有向且无环的。
求派出最少的机器人,去探索所有的宝藏地点。
机器人最初可在任意位置,然后沿路探索,但机器人不可返回。有些点可重复访问。 


解题思路:
求DAG最小路径覆盖。
因为有些点可重复访问,所以最小路径是可相交的。
要用Floyd进行缩点(传递闭包),将间接连通的点转为直接连接。
构成新图,转换为求一般的最小路径覆盖。


最小路径覆盖 = 顶点数 - 最大匹配(匈牙利算法)。


#include <cstdio>#include <cstring>const int maxn = 550;bool road[maxn][maxn],vis[maxn];int point[maxn];int n,m;void init(){int i,si,ei;memset(road,0,sizeof(road));memset(point,0,sizeof(point));for(i=0;i<m;++i){scanf("%d%d",&si,&ei);road[si][ei] = true;}}void floyd()//因为中间点可重复访问,则用floyd进行缩点,间接连通转为直接连通 {int i,j,k;for(k=1;k<=n;++k){for(i=1;i<=n;++i){for(j=1;j<=n;++j){if(road[i][k]+road[k][j]==2)road[i][j]=1;}}}}bool find(int p)//再用匈牙利算法求最大匹配 {int i;for(i=1;i<=n;++i){if(road[p][i]==true&&!vis[i]){vis[i]=true;if(point[i]==0||find(point[i])){point[i]=p;return true;}}}return false;}int main(){int i,count,ans;while(scanf("%d%d",&n,&m)){if(n==0&&m==0)break;if(m==0){printf("%d\n",n);continue;}init();floyd();count=0;for(i=1;i<=n;++i){memset(vis,0,sizeof(vis));if(find(i))count++;}ans = n - count;//最后求最小路径覆盖 printf("%d\n",ans);}return 0;}


0 0