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. 技本功丨用短平快的方式告诉你:Flink-SQL的扩展实现

    2019年1月28日,阿里云宣布开源“计算王牌”实时计算平台Blink回馈给ApacheFlink社区.官方称,计算延迟已经降到毫秒级,也就是你在浏览网页的时候,眨了一下眼睛,淘宝.天猫处理的信息已经 ...

  2. 王者荣耀交流协会--第3次Scrum会议

    Scrum master:王玉玲 要求1:工作照片 要求2:时间跨度:2017年10月15号  6:00--6:24  共计24min要求3:地点:传媒西楼204,会议室要求4:立会内容:1.从昨日会 ...

  3. FIsherman丶Team

    小组成员:郝恒杰,洪佳兴,张子祥 组长:郝恒杰 项目:Fisher Job(渔夫兼职) 简介: 我们的产品渔夫兼职是为了解决大学生兼职群体 的痛苦,他们需要一个好的渠道去找一个让自己满意的兼职,但是现 ...

  4. ImportError: No module named examples.tutorials.mnist

    Traceback (most recent call last):   File "nearest_neighbor.py", line 14, in <module> ...

  5. this & super

    /* 当本类的成员和局部变量同名用this区分. 当子父类中的成员变量同名用super区分父类.   this和super的用法很相似.   this:代表一个本类对象的引用. super:代表一个父 ...

  6. QTcpServer实现多客户端连接

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QTcpServer实现多客户端连接     本文地址:https://www.techiel ...

  7. 找xpath好用的工具(比较少用,针对只能在IE上打开的网站)

    有一些网站只能在IE浏览器里打开,不像firefox那样有好多好用的插件来找元素的xpath,css path等. 当然现在IE也可以,F12出现像firebug那样的窗口,来查看元素. 这里呢在介绍 ...

  8. 基于Git制作电子书 GitBook

    GitBook 详细介绍 GitBook 是一个基于 Node.js 的命令行工具,可使用 Github/Git 和 Markdown 来制作精美的电子书,GitBook 并非关于 Git 的教程. ...

  9. HSF源码剖析

    前言 HSF是一个分布式的远程服务调用框架,其实我更喜欢把分布式几个字去掉,因为HSF本身并不是一个单独的服务(指一个进程),他是附属在你的应用里的一个组件,一个RPC组件(远程过程调用——Remot ...

  10. 第131天:移动web页面的排版与布局

    一.总之一句话, 尽量用mm 毫米作为标准单位. 采用新的相对单位 rem 首先设置html的 font-size 为根大小. html{ font-size:1mm; } .titleheight{ ...