hdu 4289 Control 最大流+拆點

Control

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


 

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,每個城市你都可以安排特工(當然需要一定的花費,特工不會白乾活),當盜賊經過這個城市的時候就會被抓住,現在需要你花最少的錢安排特工,使得盜賊不管怎麼走都會被抓住。

 

做法:

       普通的最大流是邊上帶權,但是這道題的權值在點上,所以我們需要拆點把點拆開,當作是經過這個點就要花費這麼多權值。本題把花費當作流量,就是要求最小割,因爲最小割等於最大流,所以就是求最大流。  

        拆點就是把該點變成a\rightarrow a',邊的權值就是該點的花費,表示如果要走到這個點,則一定要有這樣的花費,再把a到b的邊變成a'\rightarrow b 和b'\rightarrow a,表示如果要從b到a的話必須要先經過b點(即已經到b')。然後就是丟進dinic跑最大流。


#include<vector>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int Ni = 200005;
const int MAX = 1<<26;
struct Edge{
    int u,v,c;
    int next;
}edge[3*Ni];
int n,m;
int edn;//邊數
int p[Ni];//父親
int d[Ni];
int sp,tp;//原點,匯點

void addedge(int u,int v,int c){
    edge[edn].u=u; edge[edn].v=v; edge[edn].c=c;
    edge[edn].next=p[u]; p[u]=edn++;

    edge[edn].u=v; edge[edn].v=u; edge[edn].c=0;
    edge[edn].next=p[v]; p[v]=edn++;
}
int bfs(){
    queue <int> q;
    memset(d,-1,sizeof(d));
    d[sp]=0;
    q.push(sp);
    while(!q.empty()){
        int cur=q.front();
        q.pop();
        for(int i=p[cur];i!=-1;i=edge[i].next){
            int u=edge[i].v;
            if(d[u]==-1 && edge[i].c>0){
                d[u]=d[cur]+1;
                q.push(u);
            }
        }
    }
    return d[tp] != -1;
}
int dfs(int a,int b){
    int r=0;
    if(a==tp)return b;
    for(int i=p[a];i!=-1 && r<b;i=edge[i].next)
    {
        int u=edge[i].v;
        if(edge[i].c>0 && d[u]==d[a]+1)
        {
            int x=min(edge[i].c,b-r);
            x=dfs(u,x);
            r+=x;
            edge[i].c-=x;
            edge[i^1].c+=x;
        }
    }
    if(!r)d[a]=-2;
    return r;
}
int gainp(int x,int y){
    return 2*x-y;
}
int dinic(int sp,int tp){
    int total=0,t;
    while(bfs()){
        while(t=dfs(sp,MAX))
        total+=t;
    }
    return total;
}
int main(){
    int i,u,v,c,x;
    while(~scanf("%d%d",&n,&m))
    {
        edn=0;//初始化
        memset(p,-1,sizeof(p));
        sp=0,tp=n*2+2;
        scanf("%d%d",&u,&v);
        addedge(sp,gainp(u,0),MAX);
        addedge(gainp(v,1),tp,MAX);
        for(int i=1;i<=n;i++){
            scanf("%d",&c);
            addedge(gainp(i,0),gainp(i,1),c);
        }
        for(int i=0;i<m;i++){
            scanf("%d%d",&u,&v);
            addedge(gainp(u,1),gainp(v,0),MAX);
            addedge(gainp(v,1),gainp(u,0),MAX);
        }
        printf("%d\n",dinic(sp,tp));
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章