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

hdu 4289 Control (最大流)

时间:2015-07-17 10:08:04      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

hdu 4289 Control

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

题目大意:一开始以为是最小费最大流问题,但仔细看一下,发现还是一个最大流问题。

解题思路:需要拆点。因为城市本身带有权值,所以需要拆成两个点,a -> a’,权值就是该点的费用。之后的城市与城市之间的边要建双向边,比如a和b两个边,要建a’ -> b和b’ -> a权值是INF。建完图之后就是普通的最大流问题了。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 1205;
const int INF = 0x3f3f3f3f;
int n, m, s, t;
int G[N][N], F[N][N];
void input() {
    memset(G, 0, sizeof(G));
    int cos;
    for (int i = 1; i <= n; i++) {
        scanf("%d", &cos);
        G[i][i + n] = cos;      
    }
    int a, b;
    for (int i = 0; i < m; i++) {
        scanf("%d %d", &a, &b);
        G[a + n][b] = INF;
        G[b + n][a] = INF;
    }
}
int maxFlow() {
    memset(F, 0, sizeof(F));
    int a[N], pre[N];   
    int ans = 0;
    queue<int> Q;
    while (1) {
        memset(a, 0, sizeof(a));
        memset(pre, 0, sizeof(pre));
        a[s] = INF;
        Q.push(s);
        while (!Q.empty()) {
            int u = Q.front(); Q.pop(); 
            for (int v = 0; v < n * 2 + 1; v++) {
                if (!a[v] && G[u][v] > F[u][v]) {
                    pre[v] = u;
                    Q.push(v);
                    a[v] = min(a[u], G[u][v] - F[u][v]);    
                }
            }
        }
        if (a[t + n] == 0) break;
        ans += a[t + n];
        for (int u = t + n; u != s; u = pre[u]) {
            F[pre[u]][u] += a[t];   
            F[u][pre[u]] -= a[t];
        }
    }
    return ans;
}
int main() {
    while (scanf("%d %d", &n, &m) == 2) {
        scanf("%d %d", &s, &t);     
        input();
        printf("%d\n", maxFlow());
    }   
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu 4289 Control (最大流)

标签:

原文地址:http://blog.csdn.net/llx523113241/article/details/46917249

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