HDU 4289 Control 网络流

题目描述:

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 

题目分析:

本体题意是在一个n个城市,m条路中,有一个犯罪团伙要从s城市到达d城市,你可以在每个城市布置一定的警力以防止团伙走过这个城市。问,在防止团伙到达d城市的前提下,你需要最少布置多少警力?
求最小割(拆点,将在点上的权转化至边上,将拆点问题转化为边)。最小割等于最大流!这道题于是也就成了一道网络流的模板题…
本题我采用dinic算法,当然有很多不同的算法都能解。因为我还不会这个算法,所以用这个写了下。
dinic算法传送门:
http://blog.csdn.net/wall_f/article/details/8207595

代码如下:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const double eps = 1e-4;
const int MAXN=100010;

struct node
{
    int v,w,next;
}mp[MAXN<<3];
int head[MAXN];
int id;
int level[MAXN];
int q[MAXN];
int n,m,s,d;

void init()
{
    memset(head,-1,sizeof(head));
    id=0;
}

void addedge(int u,int v,int w)
{
    mp[id].v=v;
    mp[id].w=w;
    mp[id].next=head[u];
    head[u]=id++;

    mp[id].v=u;
    mp[id].w=0;
    mp[id].next=head[v];
    head[v]=id++;
}

int bfs(int s, int t) //构建层次网络
{
    memset(level,0,sizeof(level));
    level[s]=1;
    int front=0,rear=1;
    q[front]=s;
    while(front < rear)
    {
        int x=q[front++];
        if(x==t) return 1;
        for(int e=head[x]; e!=-1; e=mp[e].next)
        {
            int v=mp[e].v, f=mp[e].w;
            if(!level[v] && f)
            {
                level[v]=level[x]+1;
                q[rear++]=v;
            }
        }
    }
    return 0;
}

int dfs(int u,int maxf,int t)
{
    if(u==t) return maxf;
    int ret=0;
    for(int e=head[u]; e!=-1; e=mp[e].next)
    {
        int v=mp[e].v, f=mp[e].w;
        if(level[u]+1==level[v] && f)
        {
            int Min=min(maxf-ret,f);
            f=dfs(v,Min,t);
            mp[e].w-=f;
            mp[e^1].w+=f;
            ret+=f;
            if(ret==maxf) return ret;
        }
    }
    return ret;
}

int dinic(int s, int t) //Dinic
{
    int ans = 0;
    while(bfs(s,t))
       ans+=dfs(s,INF,t);
    return ans;
}

int main()
{
    while(~scanf("%d%d%d%d",&n,&m,&s,&d))
    {
        init();
        for(int i=1; i<=n; i++)
        {
            int x;
            scanf("%d",&x);
            addedge(i,i+n,x);//拆点,将i点分成i与i+n,点的权转化至边上
        }
        for(int i=1; i<=m; i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            addedge(a+n,b,INF);//相邻点边权为无穷大,表示不可能拆这个边
            addedge(b+n,a,INF);
        }
        int ans=dinic(s,d+n);//源点与扩展后的汇点
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值