zoukankan      html  css  js  c++  java
  • POJ-2594

    Time Limit: 6000MS   Memory Limit: 65536K
    Total Submissions: 7035   Accepted: 2860

    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

    /**
              题意:最小路径覆盖
              做法:二分图最大匹配 有向无环图的最小路径覆盖 = 该图的顶点数-该图的最大匹配。
    **/
    #include<iostream>
    #include<string.h>
    #include<stdio.h>
    #include<cmath>
    #include<algorithm>
    #include<queue>
    using namespace std;
    #define maxn 510
    int g[maxn][maxn];
    int linker[maxn];
    int used[maxn];
    int n,m;
    bool dfs(int u)
    {
        for(int v = 0; v<n; v++)
        {
            if(g[u][v] && used[v] == 0)
            {
                used[v] = 1;
                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 i=0; i<n; i++)
        {
            memset(used,0,sizeof(used));
            if(dfs(i)) res++;
        }
        return res;
    }
    
    void Floyd()
    {
        int i,j,k;
        for(i=0; i<n; i++)
        {
            for(j=0; j<n; j++)
            {
                if(g[i][j]==0)
                {
                    for(k=0; k<n; k++)
                    {
                        if(g[i][k]==1&&g[k][j]==1)
                        {
                            g[i][j]=1;
                            break;
                        }
                    }
                }
            }
        }
    }
    int main()
    {
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
        while(~scanf("%d %d",&n,&m))
        {
            if(n == 0 && m == 0) break;
            memset(g,0,sizeof(g));
            int u,v;
            for(int i=0; i<m; i++)
            {
                scanf("%d %d",&u,&v);
                u--;
                v--;
                g[u][v] = 1;
            }
            Floyd();
            int res = hungary();
            printf("%d
    ",n-res);
        }
        return 0;
    }
  • 相关阅读:
    nodejs+mongoose操作mongodb副本集实例
    创建mongodb副本集操作实例
    SpringBoot(二)Web整合开发
    SpringBoot(一)走进Springboot的世界
    Git(二)Git几个区的关系与Git和GitHub的关联
    Git(一)之基本操作详解
    HttpClient(二)HttpClient使用Ip代理与处理连接超时
    HttpClient(一)HttpClient抓取网页基本信息
    Jsoup(一)Jsoup详解(官方)
    MongoDB(一)环境搭建与初始配置
  • 原文地址:https://www.cnblogs.com/chenyang920/p/4392788.html
Copyright © 2011-2022 走看看