poj2594

来源:互联网 发布:linux vnc服务器 编辑:程序博客网 时间:2024/05/05 05:34
D - Treasure Exploration
Time Limit:6000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status

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

11

2

题意:在火星上有一些宝藏,派一些机器人去发掘宝藏,每个机器人可以单向从火星上的一些点去往其他点,不同机器人可以走相同点,希望用最少的机器人去吧所有宝藏发掘出来;

这是一道最小路径覆盖问题,但是终点可以重复利用,最小路径覆盖=点数-最大匹配,在这道题中,因为需要重复的地方只要飞过去,就可以不重复了。赋予这个能力的方法就是把所有点能间接到达的点全都改为直接到达。然后正常求最小路径覆盖即可。

ac代码:

#include <iostream>#include <stdio.h>#include <vector>#include <string.h>#include <queue>using namespace std;const int maxn=505;vector<int>G[maxn];int map[maxn][maxn];int used[maxn],linker[maxn];int N;bool dfs(int u){    for(int i=1;i<=N;i++)    {        if(!used[i]&&map[u][i])        {            used[i]=1;            if(linker[i]==-1||dfs(linker[i]))            {                linker[i]=u;                return true;            }        }    }    return false;}int solve(int n){    int res=0;    memset(linker,-1,sizeof(linker));    for(int u=1;u<=n;u++)    {        memset(used,0,sizeof(used));        if(dfs(u))res++;    }    return res;}int main(){    int n,m;    while(scanf("%d%d",&n,&m)!=-1)    {        N=n;        if(n==0&&m==0)break;        int u,v;        memset(map,0,sizeof(map));        for(int i=0; i<m; i++)        {            scanf("%d%d",&u,&v);            G[u].push_back(v);            map[u][v]=1;        }        for(int i=1;i<=n;i++)          for(int j=1;j<=n;j++)            for(int k=1;k<=n;k++)          if(k!=j&&map[j][i]&&map[i][k])            map[j][k]=1;        int ans=n-solve(n);        if(ans==0)            ans=1;        cout<<ans<<endl;        for(int i=1; i<=n; i++)            G[i].clear();    }    return 0;}


0 0
原创粉丝点击