Drainage Ditches

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 67823   Accepted: 26209

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

Source

——————————我是分割线——————————————————————————————————————————————————
水题,模板题。
网络流,最大流。
增广路算法求解。
我竟然调了两个钟头......
正解是DINIC增广,但我直接BFS增广竟然也过了……
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cassert>
#include<climits>
#define maxn 210
using namespace std;
void find();
void flow();
void update();
struct Edge
{
int c;
int f;
}edge[maxn][maxn];
int n,m;
int s,t;
int residual[maxn][maxn];
int que[maxn*maxn],head,tail;
int pre[maxn];
bool vis[maxn];
int max_flow,min_flow;
void find()
{
int i,cu;
memset(vis,false,sizeof(vis));
memset(residual,,sizeof(residual));
memset(pre,,sizeof(pre));
head=;que[head]=s;pre[s]=s;vis[s]=true;tail=;
while(head<tail&&pre[t]==)
{
cu=que[head];
for(i=;i<=n;i++)
{
if(vis[i]==false)
{
if(edge[cu][i].c-edge[cu][i].f>)
{
residual[cu][i]=edge[cu][i].c-edge[cu][i].f;
pre[i]=cu;que[tail++]=i;vis[i]=true;
}
else if(edge[i][cu].f>)
{
residual[cu][i]=edge[i][cu].f;
pre[i]=cu;que[tail++]=i;vis[i]=true;
}
}
}
head++;
}
}
void flow()
{
int i=t,j;
if(pre[i]==)
{
min_flow=;return;
}
j=0x7fffffff;
while(i!=s)
{
if(residual[pre[i]][i]<j) j=residual[pre[i]][i];
i=pre[i];
}
min_flow=j;
}
void update()
{
int i=t;
if(pre[i]==) return;
while(i!=s)
{
if(edge[pre[i]][i].c-edge[pre[i]][i].f>)
edge[pre[i]][i].f+=min_flow;
else if(edge[i][pre[i]].f>)
edge[pre[i]][i].f+=min_flow;
i=pre[i];
}
}
void solve()
{
s=;t=n;
max_flow=;
while(true)
{
find();flow();
max_flow+=min_flow;
if(min_flow>) update();
else return;
}
}
int main()
{
std::ios::sync_with_stdio(false);
int i,u,v,c;
while(scanf("%d %d",&m,&n)!=EOF)
{
memset(edge,,sizeof(edge));
for(i=;i<m;i++)
{
scanf("%d %d %d",&u,&v,&c);
edge[u][v].c+=c;
}
solve();
printf("%d\n",max_flow);
}
return ;
}

POJ 1273 Drainage Ditches题解——S.B.S.的更多相关文章

  1. poj 1273 Drainage Ditches(最大流)

    http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

  2. POJ 1273 Drainage Ditches (网络最大流)

    http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  3. POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]

    题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...

  4. poj 1273 Drainage Ditches 最大流入门题

    题目链接:http://poj.org/problem?id=1273 Every time it rains on Farmer John's fields, a pond forms over B ...

  5. poj 1273 Drainage Ditches(最大流,E-K算法)

    一.Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clove ...

  6. POJ 1273 Drainage Ditches

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 67387   Accepted: 2603 ...

  7. POJ 1273 Drainage Ditches -dinic

    dinic版本 感觉dinic算法好帅,比Edmonds-Karp算法不知高到哪里去了 Description Every time it rains on Farmer John's fields, ...

  8. Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )

    题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... #include<cstdio> #include ...

  9. POJ 1273 Drainage Ditches(网络流dinic算法模板)

    POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...

随机推荐

  1. C#开发微信门户及应用(29)--微信个性化菜单的实现

    有一段时间没有接着微信的主题继续介绍里面的功能模块了,这段时间来,微信也做了不少的变化改动,针对这些特性我全面核对了一下相关的微信公众号和企业号的接口,对原有的微信API和系统管理做了全面的更新,本随 ...

  2. Tomcat问题,不能正确访问http://localhost:8080/

    最近在学Struts2框架部分的内容,但是eclipse中配置tomcat遇到了很大的问题,当辛辛苦苦的配置完了之后,竟让连小猫的首页都不能访问,http://localhost:8080/输入了之后 ...

  3. mybatis中的查询缓存

    一: 查询缓存 Mybatis提供查询缓存,用于减轻数据压力,提高数据库压力. Mybatis提供一级缓存和二级缓存. 在操作数据库时需要构造SqlSession对象,在对象中有一个数据结构(Hash ...

  4. 【工匠大道】svn使用总结

    原文地址 SVN(Subversion)是一个自由.开源的项目源代码版本控制工具.目前,绝大多数开源软件和企业代码管理,都使用SVN作为代码版本管理软件. Subversion将文件存放在中心版本库里 ...

  5. 学习.NET是因为热爱 or 兴趣 or 挣钱?

    看到最近园子里掀起了“.NET快不行了”.“.NET工资太低了”.“转行做XX”等一系列之风,不由得想说点什么,我只是基于自己的观点,你认同或者不认同,我就是这样认为,无所谓对与错,写文章就是为了交流 ...

  6. 高性能javascript学习笔记系列(5) -快速响应的用户界面和编程实践

    参考高性能javascript 理解浏览器UI线程  用于执行javascript和更新用户界面的进程通常被称为浏览器UI线程  UI线程的工作机制可以理解为一个简单的队列系统,队列中的任务按顺序执行 ...

  7. C# 生成字符串的 CheckSum

    C# 生成字符串的 CheckSum private static string CheckSum(string message) { char[] chars = message.ToCharArr ...

  8. iOS证书和描述文件

    iOS有两种证书和描述文件: 证书类型 使用场景 开发(Development)证书和描述文件 用于开发测试,在HBuilder中打包后可在真机环境通过Safari调试 发布(Distribution ...

  9. Android中Listview展示及其优化好处

    展示效果: 中间的item条目是可以上下滑动的. 代码实现: @Override public View getView(int position, View convertView, ViewGro ...

  10. postgresql 服务器端编程之hello word

    create or replace function addjifen( iuserid text, iamout INTEGER) returns text AS \[ BEGIN return ' ...