hdu 4289 Control 【图论-网络流-最大流-拆点】

来源:互联网 发布:网络应急预案 编辑:程序博客网 时间:2024/06/05 03:43
                                    Control    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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

题目大意:有n个城市,m条公路线,现在一群恐怖分子准备从S城市运输炸弹到D城市,安全局了解到情况,准备出动警员拦截这群恐怖分子,先给出每个城市的警员部署量,让你求出完成这次任务的最少部署警员量

解题思路:拆点-最大流

  1. 把每个城市拆成两个点 c-c`->w 即表示该城的警员部署量
  2. 各个城市的连接为出 c1`-c2->INF 即表示不同城市之间为INF
  3. 源点为S,汇点为D’

AC代码:

//Dinic         AC# include <iostream># include <cstring># include <queue>using namespace std;# define MAXN 505# define MAXM 200005# define INF 1 << 29struct EDGE{    int to;    int w;    int next;}edge[MAXM];int tot;int head[MAXN];int level[MAXN];int min(int a, int b){    return a > b ? b : a;}void Init(){    tot = 0;    memset(head, -1, sizeof(head));}void Addedge(int u, int v, int w){    edge[tot].to = v;    edge[tot].w = w;    edge[tot].next = head[u];    head[u] = tot++;    edge[tot].to = u;    edge[tot].w = 0;    edge[tot].next = head[v];    head[v] = tot++;}bool Bfs(int s, int t){    memset(level, 0, sizeof(level));    queue<int> que;    level[s] = 1;    que.push(s);    while (!que.empty())    {        int u = que.front(); que.pop();        for (int i = head[u]; i != -1; i = edge[i].next)        {            int v = edge[i].to;            if (level[v] == 0 && edge[i].w > 0)            {                level[v] = level[u] + 1;                if (v == t)                {                    return true;                }                que.push(v);            }        }    }    return false;}int Dfs(int u, int t, int f){    if (u == t)    {        return f;    }    int cost = 0;    for (int i = head[u]; i != -1; i = edge[i].next)    {        int v = edge[i].to;        int w = edge[i].w;        if (level[v] == level[u] + 1 && w > 0)        {            int d = Dfs(v, t, min(w, f - cost));            if (d > 0)            {                edge[i].w -= d;                edge[i ^ 1].w += d;                cost += d;                if (cost == f)                {                    break;                }                else                {                    level[v] = -1;                }            }        }    }    return cost;}int Dinic(int s, int t){    int maxflow = 0;    while (Bfs(s, t))    {        maxflow += Dfs(s, t, INF);    }    return maxflow;}int main(void){    int n, m;    while (~scanf("%d %d", &n, &m))    {        int i;        int s, t;        int u, v, w;        Init();        scanf("%d %d", &s, &t);        t = t + n;        for (i = 1; i <= n; i++)        {            scanf("%d", &w);            //拆点 把一个城市拆成两个,这两点的容量就为该点的消费量            Addedge(i, i + n, w);         }        for (i = 1; i <= m; i++)        {            scanf("%d %d", &u, &v);            Addedge(u+n, v, INF); //不同城市之间的流量为INF            Addedge(v+n, u, INF);        }        printf("%d\n", Dinic(s, t));    }    return 0;}
0 0
原创粉丝点击