zoukankan      html  css  js  c++  java
  • Treasure Exploration POJ

    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
    题意:在外星上有n个点需要机器人去探险,有m条单向路径。问至少需要几个机器人才能遍历完所有的点,一个点可以被多个机器人经过(这就是和单纯的最小路径覆盖的区别)。
    思路:这是个最小路径覆盖问题,但是因为有的点可以重复访问,所以最小路径是可以相交的,我们就用传递闭包建立新图(G’),转化为一般的路径覆盖,然后就是跟 poj1422 一样了。
       最小路径覆盖 = 图的顶点数 – 最大匹配数,所以只要用匈牙利算法求出最大匹配数,然后用顶点数一减就出来了。
    AC代码:
     1 #include<vector>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<iostream>
     6 using namespace std;
     7 #define maxn 666
     8 int match[maxn];
     9 int vis[maxn];
    10 int e[maxn][maxn];
    11 int n,m;
    12 int dfs(int u){
    13     for(int i=1;i<=n;i++){
    14         if(!vis[i]&&e[u][i]){
    15             vis[i]=1;
    16             if(match[i]==0||dfs(match[i])){
    17                 match[i]=u;
    18                 return 1;
    19             }
    20         }
    21     }
    22     return 0;
    23 }
    24 void floyd(){
    25     for(int k=1;k<=n;k++)    
    26         for(int i=1;i<=n;i++)
    27             for(int j=1;j<=n;j++)
    28                 if(e[i][k]&&e[k][j])
    29                     e[i][j]=1;
    30 }
    31 int main(){
    32         while(~scanf("%d%d",&n,&m)&&(n+m)){
    33             int x,y;
    34             memset(match,0,sizeof(match));
    35         
    36             for(int i=1;i<=m;i++){
    37                 scanf("%d%d",&x,&y);
    38                 e[x][y]=1;
    39             }
    40             floyd();
    41             int ans=0;
    42             for(int i=1;i<=n;i++){
    43                 memset(vis,0,sizeof(vis));
    44                 if(dfs(i))
    45                     ans++;
    46             }
    47             int res=n-ans;
    48             printf("%d
    ",res);
    49             for(int i=0;i<=maxn;i++)
    50                 for(int j=0;j<=maxn;j++)
    51                     e[i][j]=0;
    52         }
    53     return 0;
    54 }


  • 相关阅读:
    C#算法,二叉树,单链表,反向链表,stack栈
    系统架构师学习笔记_第十一章(上)_连载
    系统架构师学习笔记_第四章(下)_连载
    [项目过程中所遇到的各种问题记录]ORM篇——使用NHibernate配置对象实体的一些小问题
    含HTML标记的内容分页 (C#)
    PowerShell在SharePoint 2010自动化部署中的应用(1)代码获取
    走向ASP.NET架构设计第一章:走向设计
    asp.net 遍历XML文件节点信息
    系统架构师学习笔记_第一章_连载
    AOP 你想干什么 IOC 你服务什么
  • 原文地址:https://www.cnblogs.com/pengge666/p/11627356.html
Copyright © 2011-2022 走看看