HDU 4289 Control 最小割最大流 拆点
2016-05-13 20:03
736 查看
给出一个无向图,去掉每一个点都有一定的花费,给出一个起点一个终点,问最少花费多少使得这两个点之间不连通。
实际上题目就是让我们求一个无向图的最小割,我们可以利用一个定理,即最大流最小割定理,不清楚的可以看看。
于是,我们就需要求这个图的最大流,但是图中的权值在点上,因此我们需要拆点,把点上的权值转移到边上,我们假设一个点是p,我们将它拆成 p->p',这样的话当我们连接两个点p、q的时候,我们实际上需要加上<p',q>、<q',p>这两条边。因为题目没有限制一条路可以走多少次,所以其他边的容量设置为无穷就行。
Problem - 4289
查看原文:http://colorfulshark.cn/wordpress/control-1026.html
实际上题目就是让我们求一个无向图的最小割,我们可以利用一个定理,即最大流最小割定理,不清楚的可以看看。
于是,我们就需要求这个图的最大流,但是图中的权值在点上,因此我们需要拆点,把点上的权值转移到边上,我们假设一个点是p,我们将它拆成 p->p',这样的话当我们连接两个点p、q的时候,我们实际上需要加上<p',q>、<q',p>这两条边。因为题目没有限制一条路可以走多少次,所以其他边的容量设置为无穷就行。
Problem - 4289
ControlTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2634 Accepted Submission(s): 1144 [align=left]Problem Description[/align] 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 [align=left]Input[/align] 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). [align=left]Output[/align] 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. [align=left]Sample Input[/align] 5 6 5 3 5 2 3 4 12 1 5 5 4 2 3 2 4 4 3 2 1 [align=left]Sample Output[/align] 3 [align=left]Source[/align] 2012 ACM/ICPC Asia Regional Chengdu Online |
#include <iostream> #include <stdio.h> #include <string.h> #include <queue> using namespace std; const int MAXN=10001; const int MAXM=4000100; const int INF=99999999; struct Edge { int to,next,cap,flow; }; Edge edge[MAXM]; int head[MAXN],tol; int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN]; int num; void init() { tol=0; memset(head,-1,sizeof(head)); } void addedge(int u,int v,int w,int rw=0) { edge[tol].to=v; edge[tol].cap=w; edge[tol].next=head[u]; edge[tol].flow=0; head[u]=tol++; edge[tol].to=u; edge[tol].cap=rw; edge[tol].next=head[v]; edge[tol].flow=0; head[v]=tol++; } int sap(int start,int end,int N) { memset(gap,0,sizeof(gap)); memset(dep,0,sizeof(dep)); memcpy(cur,head,sizeof(head)); int u=start; pre[u]=-1; gap[0]=N; int ans=0; while(dep[start]<N) { if(u==end) { int Min=INF; for(int i=pre[u];i!=-1;i=pre[edge[i^1].to]) if(Min>edge[i].cap-edge[i].flow) Min=edge[i].cap-edge[i].flow; for(int i=pre[u];i!=-1;i=pre[edge[i^1].to]) { edge[i].flow+=Min; edge[i^1].flow-=Min; } u=start; ans+=Min; continue; } bool flag=false; int v; for(int i=cur[u];i!=-1;i=edge[i].next) { v=edge[i].to; if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u]) { flag=true; cur[u]=pre[v]=i; break; } } if(flag) { u=v; continue; } int Min=N; for(int i=head[u];i!=-1;i=edge[i].next) if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min) { Min=dep[edge[i].to]; cur[u]=i; } gap[dep[u]]--; if(!gap[dep[u]]) return ans; dep[u]=Min+1; gap[dep[u]]++; if(u!=start) u=edge[pre[u]^1].to; } return ans; } void show(int N) { for(int i=0;i<N;i++) { cout<<i+1<<":"; for(int j=head[i];j!=-1;j=edge[j].next) { int k=edge[j].to; cout<<k+1<<" "; } cout<<endl; } } int main() { int n,M,S,D; int source,sink; int cost,u,v; int ans; while(~scanf("%d%d",&n,&M)) { init(); scanf("%d%d",&S,&D); source=S-1; sink=D-1; for(int i=0;i<n;i++) { scanf("%d",&cost); addedge(i,i+n,cost); } for(int i=0;i<M;i++) { scanf("%d%d",&u,&v); u--,v--; addedge(u+n,v,INF); addedge(v+n,u,INF); } //show(2*n); ans=sap(source,sink+n,2*n); printf("%d\n",ans); } return 0; }
查看原文:http://colorfulshark.cn/wordpress/control-1026.html
相关文章推荐
- 屏幕截图程序(WTL)
- java并发编程之3——Java锁的分析
- leetcode Odd Even Linked List
- MySQL多表连接查询
- Android内核开发:图解Android系统的启动过程
- 线程简介
- spark笔记-MLlib之kmeans
- Android BitmapShader实现圆形和圆角图片
- 在springmvc中使用hibernate-validate
- Android内核开发:系统分区与镜像文件的烧写
- 基于线上环境的jvm堆栈分析,jmap+MAT
- 视觉SLAM中的数学基础 第一篇 3D空间的位置表示
- 周五脑补下几天的项目记录
- @Autowired注入原则
- Simultaneous Localization and Mapping (SLAM)讲义1
- extjs datefield 设置显示和编辑格式为‘Y-m-d’
- Android内核开发:系统编译输出的镜像文件
- MySQL Linux nginx总结
- API测试利器postMan 使用教程
- make对命令返回值的检测