HDU4289Control【最大流已知点权拆点】

来源:互联网 发布:excel求逆矩阵 编辑:程序博客网 时间:2024/04/27 14:57

Description

  You, the head of Department of Security, recently received a top-secret information that a group of terrorists is planning to transport some WMD1 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
 

这个题是暑假集训的URAL1277Cops and Thieves Dinic最大流一模一样==

拆点的时候方法不唯一,只要头尾与各边能对应就好,建议+n不用*2+1    ==

这个题数组大小要严格控制,否则要么mle 要么re

#include <iostream>#include<cstdio>#include<cstring>using namespace std;const int mm=220005;const int mn=22222;const int oo=1000000000;int node,src,dest,edge;int reach[mm],flow[mm],nxt[mm];int head[mn],work[mn],dis[mn],q[mn];inline int min(int a,int b){    return a<b?a:b;}inline void prepare(int _node,int _src,int _dest){    node=_node,src=_src,dest=_dest;    memset(head,-1,sizeof(head));    edge=0;}inline void addedge(int u,int v,int c1,int c2){    reach[edge]=v,flow[edge]=c1,nxt[edge]=head[u],head[u]=edge++;    reach[edge]=u,flow[edge]=c2,nxt[edge]=head[v],head[v]=edge++;}bool Dinic_bfs(){    int i,u,v,l,r=0;    for(i=0;i<node;++i)dis[i]=-1;    dis[q[r++]=src]=0;    for(l=0;l<r;++l)        for(i=head[u=q[l]];i>=0;i=nxt[i])            if(flow[i]&&dis[v=reach[i]]<0)            {                dis[q[r++]=v]=dis[u]+1;                if(v==dest)return 1;            }    return 0;}int Dinic_dfs(int u,int exp){    if(u==dest)return exp;    for(int &i=work[u],v,tmp;i>=0;i=nxt[i])        if(flow[i]&&dis[v=reach[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)        {            flow[i]-=tmp;            flow[i^1]+=tmp;            return tmp;        }dis[u]--;    return 0;}int Dinic_flow(){    int i,ret=0,delta;    while(Dinic_bfs())    {        for(i=0;i<node;++i)work[i]=head[i];        while(delta=Dinic_dfs(src,oo))ret+=delta;    }    return ret;}int main(){   // freopen("cin.txt","r",stdin);    int n,m,s,d;    while(~scanf("%d%d",&n,&m))    {        scanf("%d%d",&s,&d);        //s--;d--;        prepare(n*2+1,s,d+n);        for(int i=1;i<=n;i++)        {            int w;            scanf("%d",&w);          //  addedge(i<<1|1,i<<1,w,0);            addedge(i,i+n,w,0);        }        for(int i=0;i<m;i++)        {            int a,b;            scanf("%d%d",&a,&b);            //a--;b--;            addedge(a+n,b,oo,0);            addedge(b+n,a,oo,0);        }        printf("%d\n",Dinic_flow());    }    return 0;}


0 0