pku2594 Treasure Exploration

题意:给一个有向无环图,求最小的路径数覆盖每个顶点至少一次

分析:很明显的一道最小路径覆盖,可是稍微有些不同的是:“You should notice that the roads of two different robots may contain some same point. ”

标准的最小路径覆盖是不允许有相交的路径,所以将所以有可以相连的间接的路径直接相连起来,即在求最大匹配之前,用一次floyd 算法即可

#include<iostream>
using namespace std;
bool map[501][501],vis[501];
int match[501],n;
int path(int s)
{
	for(int i=1;i<=n;i++)
		if(map[s][i]&&!vis[i])
		{
			vis[i]=1;
			if(match[i]==-1||path(match[i]))
			{
				match[i]=s;
				return 1;
			}
		}
	return 0;
}
int main()
{
	int m,a,b;
	while(scanf("%d %d",&n,&m)==2&&(m||n))
	{
		memset(map,0,sizeof(map));
		memset(match,-1,sizeof(match));
		for(int i=0;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++)
				if(map[i][k]&&map[k][j])
					map[i][j]=1;
		int ans=0;
		for(int i=1;i<=n;i++)
		{
			memset(vis,0,sizeof(vis));
			ans+=path(i);
		}
		printf("%d\n",n-ans);
	}
	return 0;
}


posted @ 2011-08-30 14:42  枕边梦  阅读(335)  评论(0编辑  收藏  举报