首页 > 代码库 > HDU 4289 Control (网络流-最小割)
HDU 4289 Control (网络流-最小割)
Control
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
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).
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.
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
Source
2012 ACM/ICPC Asia Regional Chengdu Online
Recommend
liuyiding
题目大意:
解题思路:先是n,m表示n个城市,m条边,接下来src,sink,表示一个小偷从起点到终点,接下来n行表示每个城市放置警察需要的花费,接下来m行表示m条无向边。问你阻断小偷需要多少花费?
解题代码:将n个城市拆点构边,根据最小割定理,阻断起点到终点的连同最小割的花费也就是求最大流。
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <vector> #include <cmath> #include <algorithm> using namespace std; const int INF=(1<<30); const int maxn=410,maxm=201000; struct edge{ int u,v,f,next; edge(int u0=0,int v0=0,int f0=0){ u=u0;v=v0;f=f0; } }e[maxm]; int src,sink,cnt,head[maxn]; void adde(int u,int v,int f){ e[cnt].u=u,e[cnt].v=v,e[cnt].f=f,e[cnt].next=head[u],head[u]=cnt++; e[cnt].u=v,e[cnt].v=u,e[cnt].f=0,e[cnt].next=head[v],head[v]=cnt++; } void init(){ cnt=0; memset(head,-1,sizeof(head)); } queue <int> q; bool visited[maxn]; int dist[maxn]; void bfs(){ memset(dist,0,sizeof(dist)); while(!q.empty()) q.pop(); visited[src]=true; q.push(src); while(!q.empty()){ int s=q.front(); q.pop(); for(int i=head[s];i!=-1;i=e[i].next){ int d=e[i].v; if(e[i].f>0 && !visited[d]){ q.push(d); dist[d]=dist[s]+1; visited[d]=true; } } } } int dfs(int u,int delta){ if(u==sink) return delta; else{ int ret=0; for(int i=head[u];delta && i!=-1;i=e[i].next){ if(e[i].f>0 && dist[e[i].v]==dist[u]+1){ int d=dfs(e[i].v,min(e[i].f,delta)); e[i].f-=d; e[i^1].f+=d; delta-=d; ret+=d; } } if(!ret) dist[u]=-2; return ret; } } int maxflow(){ int ret=0; while(true){ memset(visited,false,sizeof(visited)); bfs(); if(!visited[sink]) return ret; ret+=dfs(src,INF); } return ret; } int n,m; void input(){ init(); scanf("%d%d",&src,&sink); sink+=n; for(int i=1;i<=n;i++){ int x; scanf("%d",&x); adde(i,i+n,x); } for(int i=0;i<m;i++){ int u,v; scanf("%d%d",&u,&v); adde(u+n,v,INF); adde(v+n,u,INF); } } void solve(){ printf("%d\n",maxflow()); } int main(){ while(scanf("%d%d",&n,&m)!=EOF){ input(); solve(); } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。