码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4289 Control(最大流,最小割)

时间:2015-08-13 19:42:15      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

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 107.
  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

 题意:恐怖分子计划将毒品从S城市运输到E城市,警察发现后准备在途中的城市全面封锁对他们进行逮捕,他们有很多路径可供选择从S到E,且路径是双向的,但是每次封锁需要花费,现在已知在每个城市进行封锁的花费,求最小的花费和。

由于给的权值不是在边上,而是在点上,所以需要进行割点处理:

技术分享

最大流模板+拆点:

#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;

const int INF=0x3f3f3f3f;
const int N=20010;

struct node
{
    int v, fee, next;
}no[6*N];
int layer[N], head[N], k;

void Add(int a, int b, int c)
{
    no[k].v = b;
    no[k].fee = c;
    no[k].next = head[a];
    head[a] = k++;

    swap(a, b);

    no[k].v = b;
    no[k].fee = 0;
    no[k].next = head[a];
    head[a] = k++;
}

int BFS(int Start, int End)
{
    int i, u, v;
    queue<int>Q;

    memset(layer, 0, sizeof(layer));

    Q.push(Start);
    layer[Start] = 1;

    while (!Q.empty())
    {
        u = Q.front(); Q.pop();

        if (u == End) return 1;

        for (i = head[u]; i != -1; i = no[i].next)
        {
            v = no[i].v;
            if (no[i].fee && layer[v] == 0)
            {
                layer[v] = layer[u] + 1;
                Q.push(v);
            }
        }
    }

    return 0;
}

int DFS(int Start, int End, int mini)
{
    int i, v, fee = 0, ans;

    if (Start == End) return mini;

    for (i = head[Start]; i != -1; i = no[i].next)
    {
        v = no[i].v;

        if (no[i].fee && layer[v] == layer[Start]+1)
        {
            ans = min(no[i].fee, mini-fee);
            ans = DFS(v, End, ans);

            no[i].fee -= ans;
            no[i+1].fee += ans;
            fee += ans;

            if (fee == mini) break;
        }
    }

    if (fee == INF) layer[Start] = 0;

    return fee;
}

int Dinic(int Start, int End)
{
    int ans = 0;

    while (BFS(Start, End))
        ans += DFS(Start, End, INF);

    return ans;
}

int main ()
{
    int n, m, i, S, E, ans, a, b, c;

    while (scanf("%d%d", &n, &m) != EOF)
    {
        memset(head, -1, sizeof(head));
        k = 0;

        scanf("%d%d", &S, &E);
        for (i = 1; i <= n; i++)
        {
            scanf("%d", &c);
            Add(i, i+n, c); ///每个点i的权值现在保存到了i-i+n这条边上
        }
        for (i = 1; i <= m; i++)
        {
            scanf("%d%d", &a, &b);
            Add(a+n, b, INF);
            Add(b+n, a, INF); ///保证每个点之间有回路,由于是双向,所以操作两次
        }

        ans = Dinic(S, E+n); 

        printf("%d\n", ans);
    }

    return 0;
}

 

HDU 4289 Control(最大流,最小割)

标签:

原文地址:http://www.cnblogs.com/syhandll/p/4727930.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!