最小割最大流定理:(参考刘汝佳p369)增广路算法结束时,令已标号结点(a[u]>0的结点)集合为S,其他结点集合为T=V-S,则(S,T)是图的s-t最小割。

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,t。对于图中的每个点,去掉这个点都需要一定的花费,求至少多少花费才能使s和t之间不连通。

思路:最基础的拆点最大流,把每个点拆作两个点i和i0,连接 I——>I0费用为去掉这个点的花费,如果原图中有一条边a和b,则连接a0和b0。(总之这四个点连完之后必须全部在环上)对图求最大流即可。

//这道题跨越了快一个月的时间,终于搞懂了,好开心—2016.9.9 ^_^。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
#define INF 0x7fffffff struct Edge
{
int st,ed;
int c;
int next;
} edge[]; int N,M,St,Ed;
int d[],head[];
int I; void Addedge(int u,int v,int c)
{
edge[I].st=u;
edge[I].ed=v;
edge[I].c=c;
edge[I].next=head[u];
head[u]=I++; edge[I].st=v;
edge[I].ed=u;
edge[I].c=;
edge[I].next=head[v];
head[v]=I++;
} bool bfs()
{
memset(d,-,sizeof(d));
int cur;
queue<int>q;
d[St]=;
q.push(St);
while(!q.empty())
{
cur=q.front();
q.pop();
if(cur==Ed+N) return true;
for(int i=head[cur]; i!=-; i=edge[i].next)
{
if(d[edge[i].ed]==- && edge[i].c>)
{
d[edge[i].ed]=d[cur]+;
q.push(edge[i].ed);
}
}
}
return false;
} int dinic(int n,int flow)
{
if(n==Ed+N) return flow;
int a,mflow=;
for(int i=head[n]; i!=-; i=edge[i].next)
{
if(d[edge[i].ed]==d[n]+ && edge[i].c)
{
a=dinic(edge[i].ed, min(flow-mflow,edge[i].c));
edge[i].c -= a;
edge[i^].c+=a;
mflow+=a;
if(mflow==flow) break;
}
}
if(mflow==) d[n]=-;
return mflow;
} int main()
{
int a,b,x;
while(scanf("%d%d",&N,&M)!=EOF)
{
scanf("%d%d",&St,&Ed);
memset(head,-,sizeof(head));
I=;
for(int i=; i<=N; i++)
{
scanf("%d",&x);
Addedge(i,i+N,x);
}
for(int i=; i<=M; i++)
{
scanf("%d%d",&a,&b);
Addedge(N+a,b,INF);
Addedge(N+b,a,INF);
}
int ans=;
while(bfs())
ans+=dinic(St,INF);
printf("%d\n",ans);
}
return ;
}

对简单的dinic再进一步优化。

最新文章

  1. out参数,ref参数,params参数数组
  2. mssql 动态行转列。
  3. 【C#】 一些不常用,很容易混淆的知识点
  4. HDU #3333
  5. git 10.8
  6. linux配置mysql,tomcat命令vi
  7. bzoj2800
  8. RMAN 备份详解
  9. Redis的Time Event与File Event的微妙关系
  10. 【代码学习】GD库中简单的验证码
  11. eclipse自动提示功能没了的解决方法
  12. Java基本语法-----java二维数组
  13. 新手尝试Android studio连接mumu调试程序
  14. [原创]浅谈IT人如何做理财规划
  15. ES6 Rest参数
  16. PLSQL developer 连接不上64位Oracle 解决办法
  17. 深度学习课程笔记(三)Backpropagation 反向传播算法
  18. 『TensorFlow』第十一弹_队列&amp;多线程&amp;TFRecod文件_我辈当高歌
  19. 关于调试php的socket服务端中遇到的问题及解决办法
  20. canvas抛物线运动demo

热门文章

  1. Enum:EXTENDED LIGHTS OUT(POJ 1222)
  2. 表单中Readonly和Disabled的区别(转载)
  3. Tomcat中文乱码问题的原理和解决方法
  4. 【leetcode】First Missing Positive(hard) ☆
  5. xcode运行push通知总是提示输入用户名和密码
  6. CSS3实现文字抹开特效
  7. sshd_conf AllowUsers参数
  8. linux 下查mac
  9. windows重新获取IP
  10. 《CLR via C#》读书笔记(1)CLR执行模型