最大流模板题

今天补最大流,先写道模板题,顺便写点对它的理解

最大流问题就是给一个幽香有向图,每一条边有容量,问若从$s$点放水,最多会有多少水流到$t$

为了解决整个问题,第一步我们当然要找到一条路径让它达到满流的状态,这里直接xjbdfs即可

比如我们找到一条路径$1\to2\to3\to4$,流量为$2$

但是我们有可能需要“反悔”,比如不走$2\to3$而走$2\to4$

这时我们可以使用一些小技巧:添加反向边并更新正向边的容量

以后如果我们走了反向边就相当于是在“反悔”

我们把上述找一条路径的过程称为“寻找增广路”

为了避免处理环等奇怪的情况,我们在每次找增广路之前先bfs一次记录每条边边权为$1$的最短路(其实是把图分层),然后按照距离$dis$dfs,即在$u$时下一步只访问$dis[v]=dis[u]+1$的点$v$

每次bfs完要一直找增广路直到找不到为止,若bfs不能到达$t$则证明没有增广路(已经满流了)

代码挺短的2333

#include<stdio.h>
#include<string.h>
#define inf 1000000000
struct edge{
	int to,nex,cap;
}e[410];
int h[210],dis[210],q[40010],tot,n;
void add(int a,int b,int c){
	tot++;
	e[tot].to=b;
	e[tot].cap=c;
	e[tot].nex=h[a];
	h[a]=tot;
}
bool bfs(){
	int head=1,tail=1,s,i;
	q[1]=1;
	memset(dis,-1,sizeof(dis));
	dis[1]=0;
	while(head<=tail){
		s=q[head];
		head++;
		for(i=h[s];i;i=e[i].nex){
			if(dis[e[i].to]==-1&&e[i].cap>0){
				dis[e[i].to]=dis[s]+1;
				tail++;
				q[tail]=e[i].to;
			}
		}
	}
	return dis[n]>0;
}
int min(int a,int b){return a<b?a:b;}
int dfs(int x,int flow){
	if(x==n)return flow;
	int i,f;
	for(i=h[x];i;i=e[i].nex){
		if(e[i].cap>0&&dis[e[i].to]==dis[x]+1){
			f=dfs(e[i].to,min(flow,e[i].cap));
			if(f){
				e[i].cap-=f;
				e[i^1].cap+=f;
				return f;
			}
		}
	}
	return 0;
}
int main(){
	int m,i,a,b,c,ans,tmp;
	while(~scanf("%d%d",&m,&n)){
		tot=1;
		memset(h,0,sizeof(h));
		for(i=1;i<=m;i++){
			scanf("%d%d%d",&a,&b,&c);
			add(a,b,c);
			add(b,a,0);
		}
		ans=0;
		while(bfs()){
			while(tmp=dfs(1,inf))ans+=tmp;
		}
		printf("%d\n",ans);
	}
}

[HDU1532]Drainage Ditches的更多相关文章

  1. HDU1532 Drainage Ditches 网络流EK算法

    Drainage Ditches Problem Description Every time it rains on Farmer John's fields, a pond forms over ...

  2. hdu-----(1532)Drainage Ditches(最大流问题)

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. HDU1532 Drainage Ditches 【最大流量】

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. HDU1532 Drainage Ditches SAP+链式前向星

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. POJ1273&&Hdu1532 Drainage Ditches(最大流dinic) 2017-02-11 16:28 54人阅读 评论(0) 收藏

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. HDU-1532 Drainage Ditches,人生第一道网络流!

    Drainage Ditches 自己拉的专题里面没有这题,网上找博客学习网络流的时候看到闯亮学长的博客然后看到这个网络流入门题!随手一敲WA了几发看讨论区才发现坑点! 本题采用的是Edmonds-K ...

  7. HDU1532 Drainage Ditches —— 最大流(sap算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 Drainage Ditches Time Limit: 2000/1000 MS (Java/ ...

  8. HDU-1532 Drainage Ditches (最大流,EK算法模板)

    题目大意:最大流的模板题...源点是0,汇点是n-1. 代码如下: # include<iostream> # include<cstdio> # include<cma ...

  9. POJ 1273 Drainage Ditches题解——S.B.S.

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 67823   Accepted: 2620 ...

随机推荐

  1. TOJ 1005 Hero In Maze (深搜)

    描述 500年前,Jesse是我国最卓越的剑客.他英俊潇洒,而且机智过人^_^. 突然有一天,Jesse心爱的公主被魔王困在了一个巨大的迷宫中.Jesse听说这个消息已经是两天以后了,他知道公主在迷宫 ...

  2. ubuntu 玩转 nodejs

    安装nginx 首先添加nginx_signing.key(必须,否则出错) $ wget http://nginx.org/keys/nginx_signing.key $ sudo apt-key ...

  3. bzoj 1197 DP

    我们可以将这个问题转化为在n维空间中一共放m个n维球,求这m个球最多将这个空间分为不同的几个部分. 那么我们设w[i][j]代表i为空间放j个球分为的部分,那么w[i][j]=w[i][j-1]+w[ ...

  4. spring mvc 提供的几个常用的扩展点

    转载 :http://blog.csdn.net/gufachongyang02/article/details/43836105 这是spring3 mvc的核心流程图:   SpirngMVC的第 ...

  5. ie8下a标签中的图片出现边框

    1.ie8下a标签中的图片出现边框 <a href="#"><img src="horse.jpg"></a> 效果如图所示 ...

  6. linux 设备树【转】

    转自:http://blog.csdn.net/chenqianleo/article/details/77779439 [-] linux 设备树 为什么要使用设备树Device Tree 设备树的 ...

  7. linux dpm机制分析(上)【转】

    转自:http://blog.csdn.net/lixiaojie1012/article/details/23707681 1      DPM介绍 1.1        Dpm:  设备电源管理, ...

  8. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6152 Friend-Graph 暴暴暴暴力

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6152 题意:判定一个无向图是否有三个点的团或者三个点的独立集. 解法:Ramsey theorem,n ...

  9. python内建方法

    abs all any apply basestring bin bool buffer bytearray bytes callable chr classmethod cmp coerce com ...

  10. 使用OC swift 截取路径中的最后的文件名

    使用 OC swift 截取路径中的最后的文件名 如何截取下面路径中最后的文件名 AppDelegate.swift /Users/XXX/Desktop/Swift/swift02/code/02- ...