• POJ Treasure Exploration 【DAG交叉最小路径覆盖】


    传送门:http://poj.org/problem?id=2594

    Treasure Exploration
    Time Limit: 6000MS   Memory Limit: 65536K
    Total Submissions: 9802   Accepted: 3979

    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

    题意概括:

    给一个 N个节点 M 条边的有向图,机器人会沿着路径前进,问最少放多少机器人可以把所有的点走完。

    解题思路:

    如果只是按普通的最小路径覆盖会出现问题,两个可以通过一个结点(或多个)可以相连的结点 有可能不被匹配到,导致路径数不是最少的。原因就在于路径可以交叉。

    所以先Floyd跑一遍求出原图的传递闭包,再跑最小路径覆盖就能解决上面的问题了。

    AC code:

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <cmath>
     6 #define INF 0x3f3f3f3f
     7 using namespace std;
     8 const int MAXN = 510;
     9 int g[MAXN][MAXN];
    10 int linker[MAXN];
    11 bool used[MAXN];
    12 int N, M;
    13 
    14 bool Find(int x)
    15 {
    16     for(int i = 1; i <= N; i++){
    17         if(!used[i] && g[x][i]){
    18             used[i] = true;
    19             if(linker[i] == -1 || Find(linker[i])){
    20                 linker[i] = x;
    21                 return true;
    22             }
    23         }
    24     }
    25     return false;
    26 }
    27 void Floyd()
    28 {
    29     for(int i = 1; i <= N; i++){
    30         for(int j = 1; j <= N; j++){
    31             for(int k = 1; k <= N; k++){
    32                 if(g[i][k] == 1 && g[k][j] == 1) g[i][j] = 1;
    33             }
    34         }
    35     }
    36 }
    37 
    38 void init()
    39 {
    40     memset(g, 0,sizeof(g));
    41     memset(linker, -1, sizeof(linker));
    42 }
    43 int main()
    44 {
    45     int u, v;
    46     while(~scanf("%d%d", &N, &M) && (N+M)){
    47         init();
    48         while(M--){
    49             scanf("%d%d", &u, &v);
    50             g[u][v] = 1;
    51         }
    52         Floyd();
    53         int ans = 0;
    54         for(int i = 1; i <= N; i++){
    55             memset(used, 0, sizeof(used));
    56             if(Find(i)) ans++;
    57         }
    58         printf("%d
    ", N-ans);
    59     }
    60     return 0;
    61 }
    View Code
  • 相关阅读:
    HDU 5818 Joint Stacks
    HDU 5816 Hearthstone
    HDU 5812 Distance
    HDU 5807 Keep In Touch
    HDU 5798 Stabilization
    HDU 5543 Pick The Sticks
    Light OJ 1393 Crazy Calendar (尼姆博弈)
    NEFU 2016省赛演练一 I题 (模拟题)
    NEFU 2016省赛演练一 F题 (高精度加法)
    NEFU 2016省赛演练一 B题(递推)
  • 原文地址:https://www.cnblogs.com/ymzjj/p/9991880.html
Copyright © 2020-2023  润新知