zoukankan      html  css  js  c++  java
  • POJ 2594 Treasure Exploration(带交叉路的最小路径覆盖)

    题意: 
    派机器人去火星寻宝,给出一个无环的有向图,机器人可以降落在任何一个点上,再沿着路去其他点探索,我们的任务是计算至少派多少机器人就可以访问到所有的点。有的点可以重复去。
    输入数据:
    首先是n和m, 代表有n个顶点, m条边。(m和n同时为0时则输入数据结束)
    接下来m行,每行两个数字 a, b代表 从a到b可以通行。
    题目分析:
    这道题目与最小路径有一点差别,最小路径覆盖上是不存在交叉路的,但是这个题目是存在交叉路的。
    对于交叉路的处理我们可以使用Floyd闭包传递。即 i->j, j->k 那么我们建边的时候 i是可以到k的。这样再进行二分匹配就行了。

     

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<iostream>
    #include<vector>
    #include<queue>
    #include<cmath>
    using namespace std;
    #define INF 0x3fffffff
    #define maxn 1705
    int n, P[maxn], m;
    bool vis[maxn], G[maxn][maxn];
    
    bool Find(int u)
    {
        for(int i=1; i<=n; i++)
        {
            if(!vis[i] && G[u][i])
            {
                vis[i] = true;
                if(P[i] == -1 || Find(P[i]) )
                {
                    P[i] = u;
                    return true;
                }
            }
        }
        return false;
    }
    
    void Floyd()
    {
        for(int k=1; k<=n; k++)
        {
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=n; j++)
                {
                    if(G[i][k] && G[k][j])
                        G[i][j] = true;
                }
            }
        }
    }
    
    int solve()
    {
        int ans = 0;
        Floyd();
        memset(P, -1, sizeof(P));
        for(int i=1; i<=n; i++)
        {
            memset(vis, false, sizeof(vis));
            if( Find(i) )
                ans ++;
        }
        return n - ans;
    }
    
    int main()
    {
        while(scanf("%d %d",&n, &m), m+n)
        {
            int a, b;
            memset(G, false, sizeof(G));
            for(int i=0; i<m; i++)
            {
                scanf("%d %d",&a, &b);
                G[a][b] = true;
            }
            printf("%d
    ", solve() );
        }
        return 0;
    }
  • 相关阅读:
    205. Isomorphic Strings
    8 旋转数组的最小数字
    303. Range Sum Query
    70. Climbing Stairs
    HDU 5971 Wrestling Match (二分图)
    URAL 2019 Pair: normal and paranormal (STL栈)
    URAL 2021 Scarily interesting! (贪心+题意)
    URAL 2018 The Debut Album (DP)
    HDU 5236 Article (概率DP+贪心)
    HDU 5241 Friends (大数)
  • 原文地址:https://www.cnblogs.com/chenchengxun/p/4718602.html
Copyright © 2011-2022 走看看