HDU4289:Control(最小割)
Control
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5023 Accepted Submission(s): 2067
題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=4289
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
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
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
題意:
給出起點和終點城市,每個城市都有一定的權值,有一群壞人要從起點到終點,現在要在一些城市上佈置警察攔截這些壞人,問最少花費。
題解:
由於點有權值,我們考慮拆點;同時這題是雙向邊。
如果我們像以往那樣反向邊容量也為c,則有誤。
我們這樣考慮插邊:插入的反向邊容量還是0,只是這樣來構造雙向邊:u'->v , v'->u。其中u'為出度點,u為入度點。
最後跑個最大流就行了。最大流等於最小割
程式碼如下:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <queue> #define INF 99999999 using namespace std; typedef long long ll; const int N = 505,M = 1e5; int head[N],d[N]; int tot,n,m,s,t; struct Edge{ int v,next,c; }e[M]; void adde(int u,int v,int c){ e[tot].v=v;e[tot].next=head[u];e[tot].c=c;head[u]=tot++; e[tot].v=u;e[tot].next=head[v];e[tot].c=0;head[v]=tot++; } bool bfs(int S,int T){ memset(d,0,sizeof(d));d[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=e[i].next){ int v=e[i].v; if(!d[v] && e[i].c>0){ d[v]=d[u]+1; q.push(v); } } } return d[t]!=0; } int dfs(int u,int a){ int flow=0,f; if(u==t || a==0) return a; for(int i=head[u];i!=-1;i=e[i].next){ int v=e[i].v; if(d[v]!=d[u]+1) continue ; f=dfs(v,min(a,e[i].c)); if(f>0){ e[i].c-=f; e[i^1].c+=f; flow+=f; a-=f; if(a==0) break; } } if(!flow) d[u]=-1; return flow; } int Dinic(){ int max_flow=0; while(bfs(s,t)) max_flow+=dfs(s,INF); return max_flow; } int main(){ while(scanf("%d%d",&n,&m)!=EOF){ memset(head,-1,sizeof(head));tot=0; scanf("%d%d",&s,&t); t+=200; for(int i=1,c;i<=n;i++){ scanf("%d",&c); adde(i,i+200,c); } for(int i=1,u,v;i<=m;i++){ scanf("%d%d",&u,&v); adde(u+200,v,INF); adde(v+200,u,INF); } printf("%d\n",Dinic()); } return 0; }