HDU 4289 Control

Problem Description
  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
 

Input
  There are several test cases.
  The first line of a single test case contains two integer N and M ( 2 <= N <= 200; 1 <= M <= 20000), the number of cities and the number of highways. Cities are numbered from 1 to N.
  The second line contains two integer S,D ( 1 <= S,D <= N), the number of the source and the number of the destination.
  The following N lines contains costs. Of these lines the ith one contains exactly one integer, the cost of locating SA in the ith city to put it under observation. You may assume that the cost is positive and not exceeding 10 7.
  The followingM lines tells you about highway network. Each of these lines contains two integers A and B, indicating a bidirectional highway between A and B.
  Please process until EOF (End Of File).
 

Output
  For each test case you should output exactly one line, containing one integer, the sum of cost of your selected set.
  See samples for detailed information.
 

Sample Input
  
  
5 6 5 3 5 2 3 4 12 1 5 5 4 2 3 2 4 4 3 2 1
 

Sample Output
  
  
3
 

最小割等于最大流

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = 1e5 + 10;
int T, n, m, s, t, flow, x, y;

struct MaxFlow
{
    struct Edges
    {
        int end, flow;
        Edges(){}
        Edges(int end, int flow) :end(end), flow(flow){}
    }edge[maxn];
    int first[maxn], next[maxn], dis[maxn], tot;

    void clear(){ tot = 0; memset(first, -1, sizeof(first)); }

    void AddEdge(int s, int t, int flow)
    {
        edge[tot] = Edges(t, 0); next[tot] = first[s]; first[s] = tot++;
        edge[tot] = Edges(s, flow);    next[tot] = first[t]; first[t] = tot++;
    }

    bool bfs(int s, int t)
    {
        memset(dis, -1, sizeof(dis));
        queue<int> p;    p.push(s);    dis[s] = 0;
        while (!p.empty())
        {
            int q = p.front();    p.pop();
            for (int i = first[q]; i >= 0; i = next[i])
            {
                if (edge[i ^ 1].flow&&dis[edge[i].end] == -1)
                {
                    p.push(edge[i].end);
                    dis[edge[i].end] = dis[q] + 1;
                }
            }
        }
        return dis[t] != -1;
    }

    int dfs(int s, int t, int low)
    {
        if (s == t) return low;
        for (int i = first[s]; i >= 0; i = next[i])
        {
            if (dis[s] + 1 == dis[edge[i].end] && edge[i ^ 1].flow)
            {
                int x = dfs(edge[i].end, t, min(low, edge[i ^ 1].flow));
                if (x)
                {
                    edge[i].flow += x;    edge[i ^ 1].flow -= x;
                    return x;
                }
            }
        }
        return 0;
    }

    int dinic(int s, int t)
    {
        int maxflow = 0, inc = 0;
        while (bfs(s, t)) while (inc = dfs(s, t, 0x7FFFFFFF)) maxflow += inc;
        return maxflow;
    }
}solve;

int main()
{
    //scanf("%d", &T);
    while (scanf("%d%d%d%d", &n, &m, &s, &t) != EOF)
    {
        solve.clear();
        for (int i = 1; i <= n; i++)
        {
            scanf("%d", &flow);
            solve.AddEdge(i, i + n, flow);
        }
        while (m--)
        {
            scanf("%d%d", &x, &y);
            solve.AddEdge(x + n, y, 0x7FFFFFFF);
            solve.AddEdge(y + n, x, 0x7FFFFFFF);
        }
        printf("%d\n", solve.dinic(s, t + n));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值