最大网络流(Dinic)
Drainage Ditches
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8043 Accepted Submission(s): 3756
clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch.
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.
1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to
Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
50
程序:
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#define M 100005
#define inf 999999999
#pragma comment(linker, "/STACK:1024000000,1024000000")//可以有效的防止爆栈
int min(int a,int b)
{
return a<b?a:b;
}
struct st
{
int u,v,next,w;
}edge[M*2];
int head[M],t,dis[M],q[M],work[M];
void init()
{
t=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)//有向边
{
edge[t].u=u;
edge[t].v=v;
edge[t].w=w;
edge[t].next=head[u];
head[u]=t++;
edge[t].u=v;
edge[t].v=u;
edge[t].w=0;
edge[t].next=head[v];
head[v]=t++;
}
/*void add(int u,int v,int w)//无向边
{
edge[t].u=u;
edge[t].v=v;
edge[t].w=w;
edge[t].next=head[u];
head[u]=t++;
edge[t].u=v;
edge[t].v=u;
edge[t].w=w;
edge[t].next=head[v];
head[v]=t++;
}*/
int bfs(int S,int T)
{
int rear=0;
memset(dis,-1,sizeof(dis));
dis[S]=0;
q[rear++]=S;
for(int i=0;i<rear;i++)
{
for(int j=head[q[i]];j!=-1;j=edge[j].next)
{
int v=edge[j].v;
if(edge[j].w&&dis[v]==-1)
{
dis[v]=dis[q[i]]+1;
q[rear++]=v;
if(v==T)
return 1;
}
}
}
return 0;
}
int dfs(int cur,int a,int T)
{
if(cur==T)
return a;
for(int &i=work[cur];i!=-1;i=edge[i].next)//一定要这样写否则会超时
{
int v=edge[i].v;
if(edge[i].w&&dis[v]==dis[cur]+1)
{
int tt=dfs(v,min(a,edge[i].w),T);
if(tt)
{
edge[i].w-=tt;
edge[i^1].w+=tt;
return tt;
}
} }
return 0;
}
int Dinic(int S,int T)
{
int ans=0;
while(bfs(S,T))
{
memcpy(work,head,sizeof(head));
while(int tt=dfs(S,inf,T))
ans+=tt;
}
return ans;
}
int main()
{
int n,m;
while(scanf("%d%d",&m,&n)!=-1)
{
init();
int a,b,c;
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
//add(b,a,c);
}
int ans=Dinic(1,n);
printf("%d\n",ans);
}
}
最大网络流(Dinic)的更多相关文章
- POJ 1273 Drainage Ditches (网络流Dinic模板)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
- ACM/ICPC 之 有流量上下界的网络流-Dinic(可做模板)(POJ2396)
//有流量上下界的网络流 //Time:47Ms Memory:1788K #include<iostream> #include<cstring> #include<c ...
- 网络流Dinic(本篇介绍最大流)
前言:看到网上Dinic和ISAP的比较,多数人认为ISAP更快,不容易爆栈.当然,也有少数人认为,在多数情况下,Dinic比较稳定.我认为Dinic的思路比ISAP更简明,所以选择了Dinc算法 介 ...
- 模板——网络流Dinic
感谢这位大佬的博客:https://www.cnblogs.com/SYCstudio/p/7260613.html 给予了我莫大的帮助! 主要说一下网络流的几个注意点: 1.和二分图匹配相似,无法继 ...
- 最大网络流dinic
初始化flow(最大流量)为INF(极大值),建边(正向弧和反向弧) bfs寻找增广路看看有没有路,顺便进行深度标号.如果没有路直接结束输出flow. 如果有,我们按照深度dfs.dfs时注意在给正向 ...
- 网络流dinic实现总结
太羞耻了,搞了半天居然没发现自己写的不是dinic,直到被一道时限紧的题目卡掉才发现 int dfs(int now,int flow,int sum) { if(now==n) return flo ...
- poj 1459 Power Network : 最大网络流 dinic算法实现
点击打开链接 Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 20903 Accepted: ...
- POJ 1273 Drainage Ditches(网络流dinic算法模板)
POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...
- 高效的网络流dinic算法模版
#include <cstring> #include <algorithm> #include <vector> #define Maxn 120010 #def ...
- 网络流(dinic算法)
网络最大流(dinic) 模型 在一张图中,给定一个源点s,给定汇点t,点之间有一些水管,每条水管有一个容量,经过此水管的水流最大不超过容量,问最大能有多少水从s流到t(s有无限多的水). 解法 di ...
随机推荐
- 关于Cocos2d-x中init方法和onEnter方法的区别
init()和onEnter()这两个方法都是写实例化对象的类(比如继承自Node的一些类等等)的时候用到的方法. 一般都是public类型下面的 bool init(); void onEnter( ...
- web.xml配置文件元素详解
一.web.xml配置文件常用元素及其意义 1 <web-app> 2 3 <!--定义了WEB应用的名字--> 4 <display-name></disp ...
- jQuery数组处理详解(转)
1. $.each(array, [callback]) 遍历[常用] 解释: 不同于例遍 jQuery 对象的 $.each() 方法,此方法可用于例遍任何对象(不仅仅是数组哦~). 回调函数拥有两 ...
- Google Analytics10条有用教程(转)
几乎每个网站都会统计自身的浏览状况:日IP.PV.跳出率.转换率.浏览者属性等等.了解这些数据有助于更好地了解浏览者的属性.知道网站在什么地方存在缺陷,为更好地提供服务.提高网站收入都有所帮助. 对于 ...
- R语言中两个数组(或向量)的外积怎样计算
所谓数组(或向量)a和b的外积,指的是a的每个元素和b的每个元素搭配在一起相乘得到的新元素.当然运算规则也可自己定义.外积运算符为 %o%(注意:百分号中间的字母是小写的字母o).比如: > a ...
- 使用Ultra Librarian转换芯片的Altium Designer封装格式
第一步:找到对应芯片的CAD文件,以OPA350为例: http://www.ti.com/product/opa350 RE: 使用Ultra Librarian转换TI芯片的Altium De ...
- tRNA 二级结构预测可视化
tRNAdb 收录了来自104个物种的623条tRNA 序列,从数据库中下载对应物种的tRNA 序列和二级结构,以人为例 打开下面的链接 http://trna.bioinf.uni-leipzig. ...
- PHP必备知识:如何下载样式文件中的图片
<?php header("content-type:text/html;charset=utf-8"); set_time_limit(0); $styleImg = fi ...
- zookeeper命令行
ZooKeeper -server host:port cmd args stat path [watch] set path data [version] ls path [watch] delqu ...
- pcduino nfs挂在光盘
1.首先在开发板上安装nfs-common 2.在pc端安装nfs-server 3.配置nfs vim /etc/exports 添加:/home/nfsroot *(rw,sync,no_root ...