Description

Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's
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. 

Input

The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points
for those ditches. Intersection 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.

Output

For each case, output a single integer, the maximum rate at which water may emptied from the pond.

Sample Input

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

Sample Output

50

Source

网络流咩,看了好久了,今天一并刷了
E-K算法重复寻找源点s到汇点t之间的增广路径,若有。找出增广路径上每一段[容量-流量]的最小值d,若无,则结束。在寻找增广路径时,能够用BFS来找。而且更新残留网络的值(涉及到反向边)。
而找到d后。则使最大流(maxflow)值加上d,更新为当前的最大流值

<pre name="code" class="cpp">#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int INF=0x7ffffff;
const int maxn=220;
int N,M;
int r[maxn][maxn];
int pre[maxn];
bool visit[maxn];
bool bfs(int s,int t)
{
queue<int>q;
memset(pre,-1,sizeof(pre));
memset(visit,false,sizeof(visit)); pre[s]=s;
visit[s]=true;
q.push(s); int p;
while(!q.empty())
{
p=q.front();
q.pop();
for(int i=1;i<=M;i++)
{
if(r[p][i]>0&&!visit[i])
{
pre[i]=p;
visit[i]=true;
if(i==t)
return true;
q.push(i);
}
}
}
return false;
}
int solve(int s,int t)
{
int d,maxflow=0;
while(bfs(s,t))
{
d=INF;
for(int i=t;i!=s;i=pre[i])
d=min(d,r[pre[i]][i]);
for(int i=t;i!=s;i=pre[i])
{
r[pre[i]][i]-=d;
r[i][pre[i]]+=d;
}
maxflow+=d;
}
return maxflow;
}
int main()
{
while(cin>>N>>M)
{
memset(r,0,sizeof(r));
int s,e,c;
for(int i=0;i<N;i++)
{
cin>>s>>e>>c;
r[s][e]+=c;
}
cout<<solve(1,M)<<endl;
}
return 0;
}


Dinic算法:依据残留网络计算层次图,在层次图中进行DFS增广。

详见:Comzyh的博客(凝视具体,解说易懂)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
int mp[250][250];
int dis[250];
int q[2000],h,r;
int n,m,ans;
int bfs()
{
int i,j;
memset(dis,-1,sizeof(dis));
dis[1]=0;
h=0;r=1;
q[1]=1;
while(h<r)
{
j=q[++h];
for(i=1;i<=n;i++)
{
if(dis[i]<0&&mp[j][i]>0)
{
dis[i]=dis[j]+1;
q[++r]=i;
}
}
}
if(dis[n]>0) return 1;
else return 0;
}
int find(int x,int low)
{
int a;
if(x==n) return low;
for(int i=1;i<=n;i++)
{
if(mp[x][i]>0&&dis[i]==dis[x]+1&&(a=find(i,min(low,mp[x][i]))))
{
mp[x][i]-=a;
mp[i][x]+=a;
return a;
}
}
return 0;
}
int main()
{
int flow,tans;
int s,t;
while(~scanf("%d%d",&m,&n))
{
memset(mp,0,sizeof(mp));
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&s,&t,&flow);
mp[s][t]+=flow;
}
ans=0;
while(bfs())
{
if(tans=find(1,0x7ffffff))
ans+=tans;
}
printf("%d\n",ans);
}
return 0;
}


POJ 1273 Drainage Ditches(网络流,最大流)的更多相关文章

  1. poj 1273 Drainage Ditches 网络流最大流基础

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 59176   Accepted: 2272 ...

  2. poj 1273 Drainage Ditches (网络流 最大流)

    网络流模板题. ============================================================================================ ...

  3. poj 1273 Drainage Ditches(最大流)

    http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

  4. POJ 1273 Drainage Ditches (网络最大流)

    http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  5. poj 1273 Drainage Ditches【最大流入门】

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63924   Accepted: 2467 ...

  6. poj 1273 Drainage Ditches(最大流,E-K算法)

    一.Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clove ...

  7. POJ 1273 Drainage Ditches (网络流Dinic模板)

    Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...

  8. POJ 1273 Drainage Ditches 网络流 FF

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 74480   Accepted: 2895 ...

  9. POJ 1273 Drainage Ditches【最大流】

    题意:给出起点是一个池塘,M条沟渠,给出这M条沟渠的最大流量,再给出终点是一条河流,问从起点通过沟渠最多能够排多少水到河流里面去 看的紫书的最大流,还不是很理解,照着敲了一遍 #include< ...

  10. POJ 1273 Drainage Ditches【最大流模版】

    题意:现在有m个池塘(从1到m开始编号,1为源点,m为汇点),及n条有向水渠,给出这n条水渠所连接的点和所能流过的最大流量,求从源点到汇点能流过的最大流量 Dinic #include<iost ...

随机推荐

  1. HTML 5 Web Storage 使用

    在html中,可以使用 Web Storage API 来保存数据在浏览器客户端,这样可以避免重复从服务器加载数据. 有两种API, sessionStorage 和 localStorage ,它们 ...

  2. 常用位操作,写8位 12864串口模式 I2C 1302 18B20

    /*以1302为例*/ void DS1302InputByte(unsigned char d)  //实时时钟写入一字节(内部函数) {     unsigned char i;     ACC ...

  3. 利用java concurrent 包实现日志写数据库的并发处理

    一.概述 在很多系统中,往往需要将各种操作写入数据库(比如客户端发起的操作). 最简单的做法是,封装一个公共的写日志的api,各个操作中调用该api完成自己操作日志的入库.但因为入数据库效率比较低,如 ...

  4. Java I/O流-PipedInputStream、PipedOutputStream

    一.整体代码图 PipedStreamDemo.java import java.io.*; class PipedStreamDemo { public static void main(Strin ...

  5. 【Cocos2d-x游戏引擎开发笔记(25)】XML解析

    原创文章,转载请注明出处:http://blog.csdn.net/zhy_cheng/article/details/9128819 XML是一种非常重要的文件格式,由于C++对XML的支持非常完善 ...

  6. ADO.NET 1创建连接、执行命令

    一无参构造函数的形式: 创建连接.创建命令.执行命令: string connstr = @"server=.;database=TestDataBase;uid=sa;pwd=130988 ...

  7. net core 中间件详解及项目实战

    net core 中间件详解及项目实战 前言 在上篇文章主要介绍了DotNetCore项目状况,本篇文章是我们在开发自己的项目中实际使用的,比较贴合实际应用,算是对中间件的一个深入使用了,不是简单的H ...

  8. windows的消息传递--消息盒子(超详细EM_UNDO等消息)

    使用delphi的消息机制可以方便操作后台,其中重要的就是sendmessage()函数.下面讲解一下这个函数 function SendMessage(hWnd: HWND; Msg: UINT;  ...

  9. 基于visual Studio2013解决C语言竞赛题之1045打印成绩

       题目 解决代码及点评 /* 功能:用记录来描述一个学生的成绩情况,内容包括:姓名.学号.数学成绩和PASCAL成绩. 要求对一个小组的10个学生的成绩进行统计处理: 1)计算学生的总 ...

  10. mysql双机热备的配置步骤

    设置双机热备: 首先要在两台机器上建立同步用户: grant replication slave on *.* to 'repdcs'@'192.168.21.39' identified by '1 ...