HDU-4289-Control (maximum flow, split point)

link:

https://vjudge.net/problem/HDU-4289

Meaning of the questions:

You, the head of Department of Security, recently received a top-secret information that a group of terrorists is planning to transport some WMD 1 from one city (the source) to another one (the destination). You know their date, source and destination, and they are using the highway network.
  The highway network consists of bidirectional highways, connecting two distinct city. A vehicle can only enter/exit the highway network at cities only.
  You may locate some SA (special agents) in some selected cities, so that when the terrorists enter a city under observation (that is, SA is in this city), they would be caught immediately.
  It is possible to locate SA in all cities, but since controlling a city with SA may cost your department a certain amount of money, which might vary from city to city, and your budget might not be able to bear the full cost of controlling all cities, you must identify a set of cities, that:
  * all traffic of the terrorists must pass at least one city of the set.
  * sum of cost of controlling all cities in the set is minimal.
  You may assume that it is always possible to get from source of the terrorists to their destination.
------------------------------------------------------------
1 Weapon of Mass Destruction

Ideas:

Seeking the minimum of the original title cutpoint Let s and t can now be split into two points each, one inlet an outlet, connected to a side, even an undirected edges between every two points.
Became in minimal cut, according to the maximum flow, maximum flow can run.

Code:

#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 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, 0});
    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);
//        cout << res << endl;
    }
    return res;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    while (cin >> n >> m)
    {
        cin >> s >> t;
        for (int i = 0;i <= n*2;i++)
            G[i].clear();
        edges.clear();
        s = s*2-1;
        t = t*2;
        int w;
        for (int i = 1;i <= n;i++)
        {
            cin >> w;
            AddEdge(i*2-1, i*2, w);
        }
        int u, v;
        for (int i = 1;i <= m;i++)
        {
            cin >> u >> v;
            AddEdge(u*2, v*2-1, INF);
            AddEdge(v*2, u*2-1, INF);
        }
        LL res = MaxFlow();
        cout << res << endl;
    }

    return 0;
}

Guess you like

Origin www.cnblogs.com/YDDDD/p/11331155.html