zoukankan      html  css  js  c++  java
  • UVA-10480-Sabotage(最大流最小割,打印路径)

    链接:

    https://vjudge.net/problem/UVA-10480

    题意:

    The regime of a small but wealthy dictatorship has been abruptly overthrown by an unexpected rebellion. Because of the enormous disturbances this is causing in world economy, an imperialist military
    super power has decided to invade the country and reinstall the old regime.
    For this operation to be successful, communication between the capital and the largest city must
    be completely cut. This is a difficult task, since all cities in the country are connected by a computer
    network using the Internet Protocol, which allows messages to take any path through the network.
    Because of this, the network must be completely split in two parts, with the capital in one part and
    the largest city in the other, and with no connections between the parts.
    There are large differences in the costs of sabotaging different connections, since some are much
    more easy to get to than others.
    Write a program that, given a network specification and the costs of sabotaging each connection,
    determines which connections to cut in order to separate the capital and the largest city to the lowest
    possible cost.

    思路:

    求最少花费不联通,最小割,不过打印路径是最小割的一条边,不是全部边.
    所有在最后一次增广后,bfs可以得到跟源点联通和跟汇点联通的边,打印对应的即可.

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    //#include <memory.h>
    #include <queue>
    #include <set>
    #include <map>
    #include <algorithm>
    #include <math.h>
    #include <stack>
    #include <string>
    #define MINF 0x3f3f3f3f
    using namespace std;
    typedef long long LL;
    
    const int MAXN = 200+10;
    const int INF = 1e9;
    
    struct Edge
    {
        int from, to, cap;
    };
    vector<Edge> edges;
    vector<int> G[MAXN*4];
    int Dis[MAXN*4];
    int a[MAXN*4], b[MAXN*4];
    int n, m, s, t;
    
    void AddEdge(int from, int to, int cap)
    {
        edges.push_back(Edge{from, to, cap});
        edges.push_back(Edge{to, from, cap});
        G[from].push_back(edges.size()-2);
        G[to].push_back(edges.size()-1);
    }
    
    bool Bfs()
    {
        memset(Dis, -1, sizeof(Dis));
        queue<int> que;
        que.push(s);
        Dis[s] = 0;
        while (!que.empty())
        {
            int u = que.front();
            que.pop();
    //        cout << u << endl;
            for (int i = 0;i < G[u].size();i++)
            {
                Edge &e = edges[G[u][i]];
                if (e.cap > 0 && Dis[e.to] == -1)
                {
                    Dis[e.to] = Dis[u]+1;
                    que.push(e.to);
                }
            }
        }
        return Dis[t] != -1;
    }
    
    int Dfs(int u, int flow)
    {
        if (u == t)
            return flow;
        int res = 0;
        for (int i = 0;i < G[u].size();i++)
        {
            Edge &e = edges[G[u][i]];
            if (e.cap > 0 && Dis[u]+1 == Dis[e.to])
            {
                int tmp = Dfs(e.to, min(flow, e.cap));
    //            cout << "flow:" << e.from << ' ' << e.to << ' ' << tmp << endl;
                e.cap -= tmp;
                flow -= tmp;
                edges[G[u][i]^1].cap += tmp;
                res += tmp;
                if (flow == 0)
                    break;
            }
        }
        if (res == 0)
            Dis[u] = -1;
        return res;
    }
    
    int MaxFlow()
    {
        int res = 0;
        while (Bfs())
        {
            res += Dfs(s, INF);
        }
        return res;
    }
    
    int main()
    {
    //    freopen("test.in", "r", stdin);
        ios::sync_with_stdio(false);
        cin.tie(0);
        while (cin >> n >> m && n)
        {
            for (int i = 1;i <= n;i++)
                G[i].clear();
            edges.clear();
            s = 1, t = 2;
            int u, v, w;
            for (int i = 1;i <= m;i++)
            {
                cin >> u >> v >> w;
                AddEdge(u, v, w);
                a[i] = u, b[i] = v;
            }
            int res = MaxFlow();
            for (int i = 1;i <= m;i++)
            {
                if ((Dis[a[i]] >= 0 && Dis[b[i]] == -1) || (Dis[b[i]] >= 0 && Dis[a[i]] == -1))
                    cout << a[i] << ' ' << b[i] << endl;
            }
            cout << endl;
        }
    
        return 0;
    }
    
  • 相关阅读:
    Hdoj 1203.I NEED A OFFER! 题解
    Hdoj 4508.湫湫系列故事——减肥记I 题解
    Hdoj 2191.悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 题解
    Hdoj 1248.寒冰王座 题解
    idea配置(卡顿、开发环境等配置),code style template
    经典收藏链接(C#总结)
    C#设计模式:原型模式(Prototype)及深拷贝、浅拷贝
    值类型和引用类型深入理解
    Resharper的配置和使用
    批处理bat 命令
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11332344.html
Copyright © 2011-2022 走看看