最小割最大流定理:(参考刘汝佳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. sqlserver insert 存储过程

    -- 根据表中数据生成insert语句的存储过程Create Proc proc_insert (@tablename varchar(256))  as                        ...

  2. Android 启动白屏或者黑屏闪现解决

    1.设置Style //1.设置背景图Theme <style name="Theme.AppStartLoad" parent="android:Theme&qu ...

  3. 【leetcode】Sum Root to Leaf Numbers(hard)

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  4. Spring控制反转与依赖注入(IOC、DI)

    IOC: 反转控制   Inverse Of Control DI:依赖注入 Dependency Injection 目的:完成程序的解耦合 解释:在应用系统的开发过程中,有spring负责对象的创 ...

  5. C++静态代码分析工具对比cppCheck与PreFast

    具体内容参看文件<CppCheck和PreFast对Cplusplus代码静态分析测试.zip> C++测试源代码main.cpp #define NULL 0 #include < ...

  6. MVC控制下输出图片、javascript与json格式

    /// <summary> /// 输出图片 /// </summary> /// <returns></returns> public ActionR ...

  7. python基础——使用元类

    python基础——使用元类 type() 动态语言和静态语言最大的不同,就是函数和类的定义,不是编译时定义的,而是运行时动态创建的. 比方说我们要定义一个Hello的class,就写一个hello. ...

  8. iOS应用支持IPV6,就那点事儿

    原文连接   果然是苹果打个哈欠,iOS行业内就得起一次风暴呀.自从5月初Apple明文规定所有开发者在6月1号以后提交新版本需要支持IPV6-Only的网络,大家便开始热火朝天的研究如何支持IPV6 ...

  9. Android Native jni 编程入门

    在某些情况下,java编程已经不能满足我们的需要,比如一个复杂的算法处理,这时候就需要用到jni(java native interface)技术: jni 其实就是java和c/cpp之间进行通信的 ...

  10. location url 反向代理到来机的其它端口 gitlab

    location /nexus { proxy_pass http://127.0.0.1:8081/nexus; } [root@GitMaven conf]# pwd /var/opt/gitla ...