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. application/x-www-urlencoded与multipart/form-data

    学习ajax时,学到了GET与POST两种HTTP方法,于是去W3C看了二者的区别,里面提到了二者的编码类型不同,就在网上查阅了相关资料, 在这里把我查阅到的相关结果记录在此,方便以后学习,详细了解一 ...

  2. Numpy入门笔记第一天

    # 导入包 import numpy as np # 创建一维数组 a = np.arange(5) print "一维numpy数组", a print "数组的类型& ...

  3. java读取excel或者csv时日期格式数据处理

    背景:最近写一个通过excel批量导入数据的功能,里面含有时间,但是java读取之后把时间转为了距离1990年1月1号的天数,比如excel中时间为2018/9/16 18:30,java读取之后变成 ...

  4. HttpServlet 详解(基础)

    HttpServlet详解 大家都知道Servlet,但是不一定很清楚servlet框架,这个框架是由两个Java包组成:javax.servlet和javax.servlet.http. 在java ...

  5. 团队目标WBS及具体任务分工

    • 首先我们讨论了实验第一个冲刺周期要实现的功能,我们的初期目标. •  然后我们进一步梳理了第一阶段的任务和需求. •  之后对任务进行了划分和领取. •  最后每个人对自己的任务进行了估算,并约定 ...

  6. python __call__ 函数

    __call__ Python中有一个有趣的语法,只要定义类型的时候,实现__call__函数,这个类型就成为可调用的. 换句话说,我们可以把这个类型的对象当作函数来使用,相当于 重载了括号运算符. ...

  7. 第三次c++作业

    https://github.com/egoistor/3Elevators-scheduling 老实说,因为这周时间紧张,(高数的期中考和一些奇奇怪怪的时期), 所以代码大体是有,但是很多细节处理 ...

  8. lintcode-511-交换链表当中两个节点

    511-交换链表当中两个节点 给你一个链表以及两个权值v1和v2,交换链表中权值为v1和v2的这两个节点.保证链表中节点权值各不相同,如果没有找到对应节点,那么什么也不用做. 注意事项 你需要交换两个 ...

  9. Alpha阶段敏捷冲刺 DAY5

    一.举行站立式例会 1.今天我们利用晚上的时间开展了站立会议,总结了一下之前工作的问题,并且制定了明天的计划. 2.站立式会议照片 二.团队报告 1.昨日已完成的工作 (1)改进了程序算法 (2)优化 ...

  10. week1词频统计

    使用java完成对txt格式的英文短片进行字符提取及统计. package nenu.softWareProject; import java.io.*;import java.util.*; pub ...