[kuangbin带你飞]专题十一 网络流\I 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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

/*
恐怖分子要把一个大型爆炸武器从一个城市运送到另一个城市,
需要安排警力部署城市来阻止恐怖分子,把警力安排到不同的城市花费不同,
现在给出安排警力到每个城市的花费,问最小花费多少可以使阻止恐怖分子。

其实就是有n个点和m条边,还有一个起点s,一个终点t,求使从S到T不连通的最小花费。

因为每个城市部署警力都要花费,所以把每一个城市拆成两个点,
因为城市是双向的,所以要建立双向边,
但是点已经拆了,所以建立双向边的时候要注意一下,
假设第一个城市点的编号是1,2,第二个城市的点的编号是3,4,那么建边的时候要: 1 -> 2, 2 -> 3, 3 -> 4, 4 -> 1。
这样才能建立双向边,建好边后直接跑最大流就可以了

*/ 
#include <iostream>
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#define N 405
#include <cstring>
#define ll long long
#define P pair<int, int>
#define mk make_pair
using namespace std;
struct ac{
    int v, c, pre;
}edge[20001 * 4];
int head[N], dis[N], curedge[N], cnt;
int inf = 0x3f3f3f3f;
int n, m;
void addedge(int u, int v, int c) {
    edge[cnt].v = v;
    edge[cnt].c = c;
    edge[cnt].pre = head[u];
    head[u] = cnt++;
    swap(u, v);
    edge[cnt].v = v;
    edge[cnt].c = 0;
    edge[cnt].pre = head[u];
    head[u] = cnt++;
}
bool bfs(int s, int e) {
    queue<int> que;
    que.push(s);
    memset(dis, 0, sizeof(dis));
    dis[s] = 1;
    while (!que.empty()) {
        int t = que.front();
        que.pop();
        for (int i = head[t]; i != -1; i = edge[i].pre) {
            if (dis[edge[i].v] || edge[i].c == 0)   continue;
            dis[edge[i].v] = dis[t] + 1;
            que.push(edge[i].v);
        } 
    }
    return dis[e] != 0;
}
int dfs(int s, int e, int flow) {
    if (s == e) return flow;
    for (int &i = curedge[s]; i != -1; i = edge[i].pre) {
        if (dis[edge[i].v] == dis[s] + 1 && edge[i].c) {
            int d = dfs(edge[i].v, e, min(flow, edge[i].c));
            if (d > 0) {
                edge[i].c -= d;
                edge[i ^1].c += d;
                return d; 
            }
        }
    }
    return 0;
}
int solve(int s, int e) {
    int sum = 0;
    while (bfs(s, e)) {
        for (int i = 0; i <= n * 2; ++i) {
            curedge[i] = head[i];
        }
        int d;
        while (d = dfs(s, e, inf)) {
            sum += d;
        }
    }
    return sum;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif

    while (scanf("%d%d", &n, &m) != EOF) {
        cnt = 0;
        memset(head, -1, sizeof(head));
        int s, e;
        scanf("%d%d", &s, &e);
        // 自己到自己,流量为花费 
        for (int i = 1; i <= n; ++i) {
            int t; scanf("%d", &t);
            addedge(i, i + n, t);
        }
        // 建边 
        for (int i = 0; i < m; ++i) {
            int l, r;
            scanf("%d%d", &l, &r);
            addedge(l + n, r, inf);
            addedge(r + n, l, inf);
        }
        int ans = solve(s, e + n);
        printf("%d\n", ans);
    }


    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值