zoukankan      html  css  js  c++  java
  • POJ 2594 Treasure Exploration(最小可相交路径覆盖)题解

    题意:有n个点,m条单向边,每个机器人能沿着单向边走,能重复经过一个点,问最少几个机器人走遍n个点

    思路:原来以前学的都是不能相交的算法....可相交的做法是跑Floyd把能到达的都加上边,然后跑最小覆盖

    代码:

    #include<set>
    #include<map>
    #include<stack>
    #include<cmath>
    #include<queue>
    #include<vector>
    #include<string>
    #include<cstdio>
    #include<cstring>
    #include<sstream>
    #include<iostream>
    #include<algorithm>
    typedef long long ll;
    using namespace std;
    const int maxn = 500 + 10;
    const int MOD = 1e9 + 7;
    const int INF = 0x3f3f3f3f;
    int linker[maxn], n, m;
    int g[maxn][maxn];
    bool used[maxn];
    bool dfs(int u){
        for(int v = 1; v <= n; v++){
            if(g[u][v] && !used[v]){
                used[v] = true;
                if(linker[v] == -1 || dfs(linker[v])){
                    linker[v] = u;
                    return true;
                }
            }
        }
        return false;
    }
    int hungry(){
        int res = 0;
        memset(linker, -1, sizeof(linker));
        for(int u = 1; u <= n; u++){
            memset(used, false, sizeof(used));
            if(dfs(u)) res++;
        }
        return res;
    }
    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] = 1;
                }
            }
        }
    }
    int main(){
        while(~scanf("%d%d", &n, &m) && n + m){
            memset(g, 0, sizeof(g));
            while(m--){
                int u, v;
                scanf("%d%d", &u, &v);
                g[u][v] = 1;
            }
            floyd();
            printf("%d
    ", n - hungry());
        }
        return 0;
    }
  • 相关阅读:
    January 25th, 2018 Week 04th Thursday
    January 24th, 2018 Week 04th Wednesday
    January 23rd, 2018 Week 04th Tuesday
    January 22nd, 2018 Week 04th Monday
    January 21st, 2018 Week 3rd Sunday
    January 20th, 2018 Week 3rd Saturday
    January 19th, 2018 Week 3rd Friday
    January 18th, 2018 Week 03rd Thursday
    January 17th, 2018 Week 03rd Wednesday
    January 16th, 2018 Week 03rd Tuesday
  • 原文地址:https://www.cnblogs.com/KirinSB/p/10472998.html
Copyright © 2011-2022 走看看