hdu4289Control

Control

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3935    Accepted Submission(s): 1648


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 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个城市,现在城市S出现了一群人,他们想运送一些炸弹到D城市,警方采取封锁城市的办法来阻断暴徒,不过封锁城市是需要花费一定代价的,求阻断暴徒从S城市到达D城市的最小需要花费的代价。
思路:每一个点都带有花费,将此点拆开,权值就是花费,然后城市与城市之间的权值无穷大,然后最大流。
代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
const int maxn=10010;
const int maxm=100010;
const int inf=0x3f3f3f3f;
int n,np,nc,m;
int s,t;
int cnt;
int Next[maxm];
int depth[maxn];
int head[maxn];
int cur[maxn];
struct edge{
    int u,v,w;
}e[maxm];
void addedge(int u,int v,int w)
{
    cnt++;
    Next[cnt]=head[u];
    head[u]=cnt;
    e[cnt].u=u;
    e[cnt].v=v;
    e[cnt].w=w;
    cnt++;
    Next[cnt]=head[v];
    head[v]=cnt;
    e[cnt].u=v;
    e[cnt].v=u;
    e[cnt].w=0;
}
int bfs()
{
    memset(depth,-1,sizeof(depth));
    depth[s]=1;
    queue<int>q;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=head[u];i!=-1;i=Next[i])
        {
            int v=e[i].v;
            if(depth[v]==-1&&e[i].w>0)
            {
                depth[v]=depth[u]+1;
                q.push(v);
            }
        }
    }
    if(depth[t]==-1)return 0;
    return 1;
}
int dfs(int u,int w)
{
    if(u==t)
        return w;
    int f=0;
    for(int i=head[u];i!=-1;i=Next[i])
    {
        int v=e[i].v;
        if(depth[v]==depth[u]+1&&e[i].w>0)
        {
            int d=dfs(v,min(w-f,e[i].w));
            if(d>0)
            {
                e[i].w-=d;
                e[i^1].w+=d;
                f+=d;
                if(f==w)break;
            }
        }
    }
    if(!f)depth[u]=-2;
    return f;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int i;
        int w;
        cnt=-1;
        memset(head,-1,sizeof(head));
        scanf("%d%d",&s,&t);
        t=t+n;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&w);
            addedge(i,i+n,w);
        }
        int a,b;
        for(i=1;i<=m;i++)
        {
            scanf("%d%d",&a,&b);
            addedge(a+n,b,inf);
            addedge(b+n,a,inf);
        }
        int ans=0;
        while(bfs())
        {
            while(int d=dfs(s,inf))
            {
                ans+=d;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值