您的位置:首页 > 其它

poj 2594 Treasure Exploration 最少边覆盖+传递闭包

2016-05-10 23:20 483 查看
给出一个有向无环图,问最少多少条边就可以覆盖所有的点,很明显的最少路径覆盖问题,但是点可以重复用,这与hungary算法不太一样,我们可以这样处理,假设a要经过b点到c,但是b点已经被访问过,为了保证a顺利到c,就让a直接飞过b到c,也就是说添加一条a到c的边,这其实就是求传递闭包,我们可以通过floyd算法求出。然后ans=n-hungary()。

一开始用了邻接表的最大二分匹配,在求闭包的时候用的dfs,但是超时了,估计是求闭包的时候加边过多导致的,而且还要判断边是否重复,因此在时空复杂度允许的情况下,尽量使用邻接表就好了。
Treasure Exploration

Time Limit: 6000MSMemory Limit: 65536K
Total Submissions: 7749Accepted: 3190
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
 
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;

const int MAXN=510;
int n,v;
int g[MAXN][MAXN];

int linker[MAXN];
bool used[MAXN];

bool dfs(int u)
{
for(int v=0; v<n; v++)
{
if(g[u][v]&&!used[v])
{
used[v]=true;
if(linker[v]==-1||dfs(linker[v]))
{
linker[v]=u;
return true;
}
}
}
return false;
}

int hungary()
{
int res=0;
memset(linker,-1,sizeof(linker));
for(int u=0; u<n; u++)
{
memset(used,false,sizeof(used));
if(dfs(u))
res++;
}
return res;
}

void Floyd()
{
for(int k=0; k<n; k++)
for(int i=0; i<n; i++)
if(g[i][k])
for(int j=0; j<n; j++)
if(g[k][j])
g[i][j] = 1;
}

int main()
{
int a,b;
int ans;
while(~scanf("%d%d",&n,&v)&&(n||v))
{
memset(g,0,sizeof(g));
for(int i=0;i<v;i++)
{
scanf("%d%d",&a,&b);
a--,b--;
g[a][b]=1;
}
Floyd();
ans=n-hungary();
printf("%d\n",ans);
}
return 0;
}

 

查看原文:http://colorfulshark.cn/wordpress/treasure-explore-996.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: