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

裸的 dinic  代码如下...  可做模板 orzzzz
#include<iostream>
#include<cstring>
#include<cstdio>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<algorithm>
#include<vector>
#define INFINFE 999999999
#define N 300
using namespace std;
int G[300][300];
bool visited[300];
int layer[300];
int n,m;
bool countlayer()
{
// cout<<"***"<<endl;
//int Layer=0;
deque<int>q;
memset(layer,0xff,sizeof(layer));
layer[1]=0;
q.push_back(1);
while(!q.empty())
{
int v=q.front();
q.pop_front();
for(int j=1; j<=m; j++)
{
if(G[v][j]>0&&layer[j]==-1)
{
layer[j]=layer[v]+1;
if(j==m)
return true;
else
q.push_back(j);
}
}
}
return false;
}
int Dinic()
{
int i;
//int s;
int nmaxflow=0;
deque<int>q;
while(countlayer())
{
while(!q.empty())
q.pop_back();
q.push_back(1);
memset(visited,0,sizeof(visited));
visited[1]=1; while(!q.empty())
{
int nd=q.back();
if(nd==m)
{
int nminc=INFINFE;
int nminc_vs;
for(unsigned int i=1; i<q.size(); i++)
{
int vs=q[i-1];
int ve=q[i];
if(G[vs][ve]>0)
{
if(nminc>G[vs][ve])
{
nminc=G[vs][ve];
nminc_vs=vs;
}
}
}
nmaxflow+=nminc;
for(unsigned int i=1; i<q.size(); i++)
{
int vs=q[i-1];
int ve=q[i];
G[vs][ve]-=nminc;
G[ve][vs]+=nminc;
}
while(!q.empty()&&q.back()!=nminc_vs)
{
visited[q.back()]=0;
q.pop_back();
}
}
else
{
for(i=1; i<=m; i++)
{
if(G[nd][i]>0&&layer[i]==layer[nd]+1&&!visited[i])
{
visited[i]=1;
q.push_back(i);
break;
}
}
if(i>m)
q.pop_back();
}
}
}
return nmaxflow;
}
int main()
{
while(cin>>n>>m)
{
int i;
int s,e,c;
memset(G,0,sizeof(G));
for(i=0; i<n; i++)
{
cin>>s>>e>>c;
G[s][e]+=c;
}
cout<<Dinic()<<endl;
}
return 0;
}

poj 1273 裸 网络流 (dinic)的更多相关文章

  1. POJ 1273 Drainage Ditches -dinic

    dinic版本 感觉dinic算法好帅,比Edmonds-Karp算法不知高到哪里去了 Description Every time it rains on Farmer John's fields, ...

  2. Drainage Ditches - poj 1273(网络流模板)

    题意:1是源点,m是汇点,求出来最大流量,没什么好说的就是练习最大流的模板题 ************************************************************* ...

  3. poj 1273最大流dinic算法模板

    #include<stdio.h> #include<string.h> #define N 300 #define inf 0x7fffffff #include<qu ...

  4. POJ 1273 Drainage Ditches(网络流dinic算法模板)

    POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...

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

    链接: http://poj.org/problem?id=1273 代码: //Dinic #include<stdio.h> #include<string.h> #inc ...

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

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

  7. BZOJ1001 狼抓兔子(裸网络流)

    Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的, 而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一 ...

  8. UVA 820 --- POJ 1273 最大流

    找了好久这两个的区别...UVA820 WA了 好多次.不过以后就做模板了,可以求任意两点之间的最大流. UVA 是无向图,因此可能有重边,POJ 1273是有向图,而且是单源点求最大流,因此改模板的 ...

  9. POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]

    题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...

随机推荐

  1. JY播放器【网易云音乐破解下载】

    今天给大家带来一款神器----JY播放器.可以直接下载网易云音乐的歌曲. 目前已经支持平台(蜻蜓FM.喜马拉雅FM.网易云音乐.QQ音乐) 使用方法: 在电脑打开网易云音乐或者网站找到你要听的歌曲或歌 ...

  2. PHP手动环境搭建之WAMP

    第一步:安装apache程序 首先需要去Apache官网下载Apache2.4(http://httpd.apache.org/download.cgi),操作如下图所示: 下载完成后把它解压出来,然 ...

  3. hdfs向hbase上传数据报错分析

    通过hbse的import工具向hbase导入文件时出现出错误: hbase org.apache.hadoop.hbase.mapreduce.Driver import hbase_rgrid_k ...

  4. HDU 6438

    Problem Description The Power Cube is used as a stash of Exotic Power. There are n cities numbered 1 ...

  5. 【RL系列】SARSA算法的基本结构

    SARSA算法严格上来说,是TD(0)关于状态动作函数估计的on-policy形式,所以其基本架构与TD的$v_{\pi}$估计算法(on-policy)并无太大区别,所以这里就不再单独阐述之.本文主 ...

  6. [T-ARA][HUE]

    歌词来源:http://music.163.com/#/song?id=22704406 wa du seu mo geum to yo do ga tae 어딜가도 스페셜한게 없어 [eo-dil ...

  7. linux同步软件

    linux同步软件:scp,rsync,inotify,sersync 1.scp: scp就是secure copy,是用来进行远程文件拷贝的.数据传输使用 ssh,并且和ssh 使用相同的认证方式 ...

  8. mongodb redis memcache 对比

    从以下几个维度,对 Redis.memcache.MongoDB 做了对比. 1.性能 都比较高,性能对我们来说应该都不是瓶颈. 总体来讲,TPS 方面 redis 和 memcache 差不多,要大 ...

  9. pip安装Crypto注意事项

    pip install PyCrypto 1.使用pip install Crypto的方式安装的文件夹名称为crypto,而内部引用都用的Crypto路径,因此pip安装后,需要将文件夹名称修改为C ...

  10. Lucky Conversion(找规律)

    Description Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive int ...