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. 使用Azure人脸API对图片进行人脸识别

    人脸识别是人工智能机器学习比较成熟的一个领域.人脸识别已经应用到了很多生产场景.比如生物认证,人脸考勤,人流监控等场景.对于很多中小功能由于技术门槛问题很难自己实现人脸识别的算法.Azure人脸API ...

  2. office2010的破解工具

    office2010的破解工具,找了好多的密钥都不合适,直接用这个软件一键搞定, 下载地址:https://pan.baidu.com/s/1phPwihCDipGwGdSmjWNeYw 提取码:8m ...

  3. 001_centos7下比特币源码编译安装

    今天我们介绍比特币的源码安装过程,是利用编译安装的 首先安装依赖 yum install -y boost-devel qt-devel protobuf-devel qrencode-devel l ...

  4. wifi渗透

    前言 本文主要讲述 家庭家庭家庭中(重要的事情说三遍,企业认证服务器的wifi一般非常非常的安全破解不来)如何破解wifi密码,破解wifi密码后的内网渗透利用(简单说明),如何设置wifi路由器更安 ...

  5. 致敬平凡的程序员--《SOD框架“企业级”应用数据架构实战》自序

    “简单就是美” “平凡即是伟大” 上面两句话不知道是哪位名人说的,又或者是广大劳动人民总结的,反正我很小的时候就常常听到这两句话,这两句话也成了我的人生格言,而且事实上我也是一个生活过得比较简单的平凡 ...

  6. 【Mysql】SpringBoot_2.1.0+Druid_1.1.10 配置数据源监控服务Yml格式

    访问地址:localhost:8080/druid 按照这个方法和版本配置没问题 版本或高或低可能会出现不兼容 1.添加依赖 <dependency> <groupId>com ...

  7. 简单Web服务器

    import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.F ...

  8. dota2输入法无候选框?

    win10自带的微软拼音在dota2中不会显示候选框,或者说是选词框. 这种情况下只能选择其他输入法. 我选择的是搜狗输入法(此随笔添加时的最新版),在dota2中有选词框. 网上有些人说要改兼容性, ...

  9. excel如何复制筛选内容

    https://jingyan.baidu.com/article/ca00d56c75b7e5e99eebcf3c.html

  10. python数据类型分类(可变(不可变)数据类型)

    一:python数据类型的分类: 可变(不可哈希)的数据类型: list 列表 dict 字典 set 集合 不可变(可哈希)的数据类型: str 字符串 bool 布尔型 int 整型 tuple ...