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

代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath> typedef long long ll;
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = ;
const int maxe = * maxn * maxn;
struct MaxFlow
{
struct Edge {
int v, w, nxt;
} edge[maxe];
int head[maxn], tot, level[maxn];
void init(){
memset(head,-,sizeof(head));
tot=;
}
void add(int u, int v, int w) {
edge[tot].v = v;
edge[tot].w = w;
edge[tot].nxt = head[u];
head[u] = tot++; edge[tot].v = u;
edge[tot].w = ;
edge[tot].nxt = head[v];
head[v] = tot++;
}
bool bfs(int s, int t) {
memset(level, -, sizeof(level));
queue<int>q;
q.push(s);
level[s] = ;
while(!q.empty()) {
int u = q.front(); q.pop();
for(int i = head[u]; ~i; i = edge[i].nxt) {
if(edge[i].w > && level[edge[i].v] < ) {
level[edge[i].v] = level[u] + ;
q.push(edge[i].v);
}
}
}
return level[t] > ;
}
int dfs(int u, int t, int f) {
if(u == t) return f;
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
if(edge[i].w > && level[v] > level[u]) {
int d = dfs(v, t, min(f, edge[i].w));
if(d > ) {
edge[i].w -= d;
edge[i ^ ].w += d;
return d;
}
}
}
level[u] = -;
return ;
}
int solve(int s, int t) {
int flow = , f;
while(bfs(s, t)) {
while(f = dfs(s, t, inf)) flow += f;
}
return flow;
}
}F; int main(){
int n,m;
while(~scanf("%d%d",&m,&n))
{
F.init();
for(int i=;i<m;i++){
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
F.add(u,v,c);
}
printf("%d\n",F.solve(,n));
}
return ;
}
												

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

  1. Tile Cut~网络流入门题

    Description When Frodo, Sam, Merry, and Pippin are at the Green Dragon Inn drinking ale, they like t ...

  2. POJ 1273:Drainage Ditches 网络流模板题

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63339   Accepted: 2443 ...

  3. POJ 3207 【2-SAT入门题 + 强连通分量】

    这道题是我对于2-SAT问题的入门题:http://poj.org/problem?id=3207 一篇非常非常非常好的博客,很详细,认真看一遍差不多可以了解个大概:https://blog.csdn ...

  4. POJ 2342 树形DP入门题

    有一个大学的庆典晚会,想邀请一些在大学任职的人来參加,每一个人有自己的搞笑值,可是如今遇到一个问题就是假设两个人之间有直接的上下级关系,那么他们中仅仅能有一个来參加,求请来一部分人之后,搞笑值的最大是 ...

  5. 网络流入门—用于最大流的Dinic算法

    "网络流博大精深"-sideman语 一个基本的网络流问题 最早知道网络流的内容便是最大流问题,最大流问题很好理解: 解释一定要通俗! 如右图所示,有一个管道系统,节点{1,2,3 ...

  6. USACO 4.2 Drainage Ditches(网络流模板题)

    Drainage DitchesHal Burch Every time it rains on Farmer John's fields, a pond forms over Bessie's fa ...

  7. HDU 1532 Drainage Ditches(网络流模板题)

    题目大意:就是由于下大雨的时候约翰的农场就会被雨水给淹没,无奈下约翰不得不修建水沟,而且是网络水沟,并且聪明的约翰还控制了水的流速, 本题就是让你求出最大流速,无疑要运用到求最大流了.题中m为水沟数, ...

  8. POJ 3984(DFS入门题 +stack储存路径)

    POJ 3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, ...

  9. poj1273 网络流入门题 dinic算法解决,可作模板使用

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 62078   Accepted: 2384 ...

随机推荐

  1. slots属性(省内存,限制属性的定义)

    class Foo: __slots__=['name','age'] #{'name':None,'age':None} # __slots__='name' #{'name':None,'age' ...

  2. Node.js异步IO原理剖析

    为什么要异步I/O? 从用户体验角度讲,异步IO可以消除UI阻塞,快速响应资源 JavaScript是单线程的,它与UI渲染共用一个线程.所以在JavaScript执行的时候,UI渲染将处于停顿的状态 ...

  3. 用MPI进行分布式内存编程(1)

    <并行程序设计导论>第三章部分程序 程序3.1运行实例 #include<stdio.h> #include<string.h> #include<mpi.h ...

  4. JavaScript 实用方法

    1.按时间显示问候语 2.强制光标停留位置 3.保存页面文本 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN& ...

  5. Flutter 打包程序 build android apk

    Step-1 Java 路径 找到java路径, 可使用[flutter doctor -v] Step-2: 进入目录 找到路径后 C:\Program Files\Java\jre1.8.0_23 ...

  6. MST(最小生成树)

    1.prim算法分析 prim算法是用来构建MST(最小生成树)的一种基于贪心策略的算法.prim算法通过维护lowcost数组和closest数组记录每次查询的最小权值边结点. 首先,看一个示例来理 ...

  7. Mybatis 循环删除/插入

    <foreach collection="array" open="(" separator="," close=")&qu ...

  8. day33:进程II

    目录 1.锁:Lock 2.信号量:Semaphone 3.事件:Event 4.进程队列:Queue 5.生产者和消费者模型 6.JoinableQueue 锁:Lock 1.锁的基本概念 上锁和解 ...

  9. python 02 if while

    1. if的格式 >>> 1<3 True 真>>> 1>3False 假 if   条件:                     条件 + : (t ...

  10. 一个@Transaction哪里来这么多坑?

    前言 在之前的文章中已经对Spring中的事务做了详细的分析了,这篇文章我们来聊一聊平常工作时使用事务可能出现的一些问题(本文主要针对使用@Transactional进行事务管理的方式进行讨论)以及对 ...