最小割最大流定理:(参考刘汝佳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再进一步优化。

hdu4289 最小割最大流 (拆点最大流)的更多相关文章

  1. hdu4289(最小割)

    传送门:Control 题意:有n个城市,有个小偷想从其中一个城市逃到另一个城市,警察想要堵截这个小偷,知道了在每个城市堵截的成本,问如何安排在哪些城市堵截可以使得小偷一定会被抓住,而且成本最低. 分 ...

  2. hdu4289最小割

    最近博客断更了一段时间啊,快期末了,先把这个专题搞完再说 最小割=最大流 拆点方法很重要,刚开始我拆点不对就wa了,然后改进后tle,应该是数组开小了,一改果然是 #include<map> ...

  3. hdu 4289 最小割,分拆点为边

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2609 #include <cstdio> #incl ...

  4. hdu-4289.control(最小割 + 拆点)

    Control Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  5. 最小割最大流定理&残量网络的性质

    最小割最大流定理的内容: 对于一个网络流图 $G=(V,E)$,其中有源点和汇点,那么下面三个条件是等价的: 流$f$是图$G$的最大流 残量网络$G_f$不存在增广路 对于$G$的某一个割$(S,T ...

  6. 最小割&网络流应用

    重要链接 基础部分链接 : 二分图 & 网络流初步 zzz大佬博客链接 : 网络流学习笔记 重点内容:最小割二元关系新解(lyd's ppt) 题目:网络流相关题目 lyd神犇课件链接 : 网 ...

  7. BZOJ 3438: 小M的作物( 最小割 )

    orz出题人云神... 放上官方题解... 转成最小割然后建图跑最大流就行了... ---------------------------------------------------------- ...

  8. 最小割求法&&可行边和必须边

    最小割的可行边与必须边 就是在残量网络上跑tarjan 可行边: 满流并且残量网络上不能存在入点到出点的路径 必须边: 满流并且残量网络上入点能从源点到达,出点能到汇点. 任意一种最小割求法: 跑一边 ...

  9. SPOJ 839 Optimal Marks(最小割的应用)

    https://vjudge.net/problem/SPOJ-OPTM 题意: 给出一个无向图G,每个点 v 以一个有界非负整数 lv 作为标号,每条边e=(u,v)的权w定义为该边的两个端点的标号 ...

随机推荐

  1. Android屏幕适配dp、px两套解决办法

    "又是屏幕适配,这类文章网上不是很多了吗?" 我也很遗憾,确实又是老问题.但本文重点对网上的各种方案做一个简短的总结,和具体使用方法. 若想了解具体Android设备适配的前世因果 ...

  2. 【leetcode】Regular Expression Matching (hard) ★

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  3. 【linux】学习3

    鸟哥 书的第7章 从 /home/dtest1   跳入 /home/dtest2 目录: cd  ../dtest2   注意 cd后有空格 ..后无空格 特殊目录: .    代表此层目录 .. ...

  4. 【Git】自定义Git

    来源:廖雪峰 让Git显示颜色 git config --global color.ui true 忽略特殊文件 有些文件有敏感信息,或者是自动生成的中间文件.不能或不必提交到git,可以用.giti ...

  5. JavaScript for循环里边异步操作问题。

    问题:(DRIVING.search是异步操作) for循环中做异步操作会导致aDistances数组里边的数据全部都是从A_SHOP_INFO数组中最后一条数据获取的值. var iIdx = 0; ...

  6. Java Collection、Map集合总结

    1.Java中的Collection集合总结 Collection |---List(存储有序,可重复) |---ArrayList 底层数据结构是数组,查询快,增删慢. 线程不安全.效率高 |--- ...

  7. October 8th 2016 Week 41st Saturday

    When ambition ends, happiness begins. 野心消亡之日,正是快乐破茧之时. If I don't have the wish to be a useful man, ...

  8. IDT5V49EE904资料学习

    一.特性: 1.4个内部PLL 2.内部非易失EEPROM. 3.最快400k的I2C串行接口. 4.输入时钟范围:1M—200M. 5.输出时钟范围:4.9k—200M 6.输入晶振参数带有在线可编 ...

  9. ios 拨打电话

    1,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示NSMutableString * str=[[NSMutableString alloc] initWithFo ...

  10. Pyqt 屏幕截图工具

    从Pyqt的examples中看到一段截图代码, (路径:examplas\desktop\screenshot.py) 所以想自己UI下界面,手动练习下 通过UI生成的: Screenshot.py ...