HDU 4289 Control (拆点+网络流)

来源:互联网 发布:988款淘宝爆款详情页 编辑:程序博客网 时间:2024/06/01 07:11

Control

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


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 65 35234121 55 42 32 44 32 1
 

Sample Output
3
 

Source
2012 ACM/ICPC Asia Regional Chengdu Online 
 

题意:
有一个恐怖分子要从a点到b点,他可能通过所有线路。在一些点中放置特工,使一定抓到恐怖分子。特工在每个点的价格不一样,求最小总价格。
POINT:
点权值,拆点,一个点分为入点和出点,入点到出点的权值为点权值。两个点连线,便是a-a’-b-b’-a.有向一圈。

#include <vector>#include <stdio.h>#include <iostream>#include <stdio.h>#include <queue>#include <string.h>using namespace std;#define LL long longtypedef pair<LL,LL> pr;const LL maxn =222*2;const LL inf = 0x3f3f3f3f;LL n,m,s,t;struct edge{    LL from,to,cap,flow;    LL nxt;}len[20000*4*2+666];LL val[maxn];LL head[maxn];LL sz;LL d[maxn];bool bfs(){    LL q[maxn<<1];    LL vis[maxn];    memset(vis,0,sizeof vis);    memset(d,0,sizeof d);    d[s]=1;    LL hd=1,wei=1;    vis[s]=1;    q[wei++]=s;    while(hd!=wei)    {        LL u=q[hd++];        for(LL i=head[u];i!=-1;i=len[i].nxt)        {            if(!vis[len[i].to]&&len[i].cap>len[i].flow)            {                vis[len[i].to]=1;                d[len[i].to]=d[u]+1;                q[wei++]=len[i].to;            }        }    }    return vis[t];}LL cur[maxn];LL dfs(LL u,LL a){    if(u==t||a==0) return a;    LL flw=0,f;    for(LL i=head[u];i!=-1;i=len[i].nxt)    {        if(d[len[i].to]-1==d[u]&&(f=dfs(len[i].to,min(a,len[i].cap-len[i].flow)))>0)        {            flw+=f;            len[i].flow+=f;            len[i^1].flow-=f;            a-=f;        }        if(a==0) break;    }    if(flw==0) d[u]=-1;    return flw;}LL maxflow(){    LL ans=0;    while(bfs())    {        ans+=dfs(s,inf);    }    return ans;    }void add(LL u,LL v,LL c){    len[sz].from=u;    len[sz].to=v;    len[sz].cap=c;    len[sz].flow=0;    len[sz].nxt=head[u];    head[u]=sz++;            len[sz].from=v;    len[sz].to=u;    len[sz].cap=0;    len[sz].flow=0;    len[sz].nxt=head[v];    head[v]=sz++;}void init(){    memset(head,-1,sizeof head);    sz=0;}int main(){    while(~scanf("%lld %lld",&n,&m))    {        init();        scanf("%lld %lld",&s,&t);        s=s*2;        t=t*2+1;        for(LL i=1;i<=n;i++)        {            scanf("%lld",&val[i]);            add(i*2,i*2+1,val[i]);        }        for(LL i=1;i<=m;i++)        {            LL u,v;scanf("%lld %lld",&u,&v);            add(u*2+1,v*2,inf);            add(v*2+1,u*2,inf);        }        printf("%lld\n",maxflow());    }}


原创粉丝点击