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. Valgrind 简单用法

    有时需要给自己写的小程序做个简单的 benchmark,查看内存使用情况和运行时间.这时可以试试 valgrind. Ubuntu 下安装很简单: sudo apt-get update sudo a ...

  2. Python基础灬高阶函数(lambda,filter,map,reduce,zip)

    高阶函数 lambda函数 关键字lambda表示匿名函数,当我们在传入函数时,有些时候,不需要显式地定义函数,直接传入匿名函数更方便. lambda函数省略函数名,冒号前为参数,冒号后函数体. # ...

  3. ASP.NET 异步Web API + jQuery Ajax 文件上传代码小析

    该示例中实际上应用了 jquery ajax(web client) + async web api 双异步. jquery ajax post $.ajax({ type: "POST&q ...

  4. 第六次ScrumMeeting博客

    第六次ScrumMeeting博客 本次会议于10月31日(二)22时整在3公寓725房间召开,持续15分钟. 与会人员:刘畅.辛德泰.窦鑫泽.张安澜.赵奕.方科栋. 除了汇报任务外,窦鑫泽同学还就前 ...

  5. 王者荣耀交流协会第一次Scrum立会

    工作照片: scrum master:高远博 时间跨度;2017/10/13 6:04-6:34 地点:一食堂二楼两张桌子旁 立会内容; 昨天的成绩;昨天商议了今天的开会的时间.地点 今天的计划;讨论 ...

  6. C语言的问卷调查

    1.你对自己的未来有什么规划?做了哪些准备? 未来想当一个网络工程师,为了这个目标我正在努力学习网络.网页及相关的知识. 2.你认为什么是学习?学习有什么用?现在学习动力如何?为什么? 学习就是不断尝 ...

  7. css全局样式基础代码

    body{ font-size:12px; font-family:"宋体",Arial, Helvetica, sans-serif;color:#363636;backgrou ...

  8. C语言之goto浅析

    1.  读代码时遇了的疑惑点: static int do_bind(const char *host, int port, int protocol, int *family) { int fd; ...

  9. DB2定位锁等待

    在应用中,我们经常会碰到sql执行很慢,但是数据库cpu和内存使用率又不高的情况,类似的问题基本上由于锁,排序等原因造成,本文主要描述如何去定位锁等待问题,谁在锁等待?等待谁持有的锁?锁在那个表? 一 ...

  10. JAVA第三次笔记