Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 54887   Accepted: 20919

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

解题报告
听说第一发网络流要纪念一下,有模板的嫌疑,Discuss的情况都遇上了。多组忘了清零,重边忘了处理。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define inf 99999999
#define M 200+10
using namespace std;
struct edge
{
int c,f;
}edge[M][M];
int n,m,vis[M],a[M],pre[M],f;
queue<int>Q;
void ford()
{
while(1)
{
memset(pre,-1,sizeof(pre));
memset(a,-1,sizeof(a));
memset(vis,-1,sizeof(vis));
while(!Q.empty())
Q.pop();
Q.push(1);
pre[1]=1;
vis[1]=0;
a[1]=inf;
while(!Q.empty()&&vis[m]==-1)
{
int u=Q.front();
Q.pop();
for(int v=1;v<=m;v++)
{
if(vis[v]==-1)
{
if(edge[u][v].f<edge[u][v].c)
{
a[v]=min(a[u],edge[u][v].c-edge[u][v].f);
vis[v]=0;
pre[v]=u;
Q.push(v);
}
else if(edge[v][u].f>0)
{
a[v]=min(a[u],edge[v][u].f);
vis[v]=0;
pre[v]=-u;
Q.push(v);
}
}
}
vis[u]=1;
}
if(vis[m]==-1||a[m]==0)
break;
int k1=m,k2=abs(pre[k1]);
int aaa=a[m];
while(1)
{
edge[k2][k1].f+=aaa;
edge[k1][k2].f-=aaa;
if(k2==1)break;
k1=k2;k2=abs(pre[k2]);
}
f+=a[m];
}
}
int main()
{
int i,j,u,v,w;
while(~scanf("%d%d",&n,&m))
{
f=0;
memset(edge,0,sizeof(edge));
for(i=0;i<n;i++)
{
scanf("%d%d%d",&u,&v,&w);
edge[u][v].c+=w;
}
ford();
printf("%d\n",f);
}
return 0;
}

POJ1273_Drainage Ditches(网络流)的更多相关文章

  1. POJ 1273 Drainage Ditches (网络流Dinic模板)

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

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

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

  3. HDU1532 Drainage Ditches 网络流EK算法

    Drainage Ditches Problem Description Every time it rains on Farmer John's fields, a pond forms over ...

  4. poj 1273 Drainage Ditches (网络流 最大流)

    网络流模板题. ============================================================================================ ...

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

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

  6. NYOJ 323 Drainage Ditches 网络流 FF 练手

    Drainage Ditches 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 Every time it rains on Farmer John's fields, ...

  7. POJ 1273 Drainage Ditches 网络流 FF

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 74480   Accepted: 2895 ...

  8. Drainage Ditches~网络流模板

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

  9. [POJ1273][USACO4.2]Drainage Ditches (网络流最大流)

    题意 网络流最大流模板 思路 EK也不会超时 所以说是一个数据比较水的模板题 但是POJ有点坑,多组数据,而且题目没给 哭得我AC率直掉 代码 用的朴素Dinic #include<cstdio ...

随机推荐

  1. php利用iframe实现无刷新文件上传功能

    上传原理很简单就是利用表单的打开方式为iframe的name名,这样就可以在当前页面的iframe打来了,实现文件上传,再利用js返回上传结果. form target .在 action 属性中规定 ...

  2. 一步一步重写 CodeIgniter 框架 (10) —— 使用 CodeIgniter 类库(续)

    上一节简单实现了 CI 的类库扩展模型,所以 _ci_load_class 和 _ci_init_class 写的不是很完备.根据上节课的分析,当 system/libraries 目录下存在 Ema ...

  3. CSipSimple最新版本号(二)--加入视频功能

    前面我们编译好了最新版本号的CSipSimple,并且測试已经能够打电话了.如今要把视频功能加上去. 不知道怎么编译的,能够看我的上一篇博文:CSipSimple最新版本号 我们先来看一下之前的项目是 ...

  4. iOS:(接口适配器3)--iPhone适应不同型号 6/6plus 前

    对于不同的苹果设备.检查每个参数<iOS:机型參数.sdk.xcode各版本号>.        机型变化 坐标:表示屏幕物理尺寸大小,坐标变大了.表示机器屏幕尺寸变大了: 像素:表示屏幕 ...

  5. AES SHA-1 SM3 MD5

    AES是美国国家标准技术研究所NIST旨在代替DES的21世纪的加密标准. 输入:128bit 的消息分组 输出:128bit 的密文分组 密钥长度及圈数 128 10 192 12 256 14 消 ...

  6. 快捷键accesskey

    <!DOCTYPE html> <html> <body> <a href="http://www.w3school.com.cn/html/&qu ...

  7. c语言数组初始化问题

    2147483648字符数组的初始化,最容易理解的方式就是逐个字符赋给数组中各元素. charstr[10]={'I','','a','m','',‘h’,'a','p','p','y'}; 即把10 ...

  8. Creating Spatial Indexes(mysql 创建空间索引 The used table type doesn't support SPATIAL indexes)

    For MyISAM tables, MySQL can create spatial indexes using syntax similar to that for creating regula ...

  9. 经验交流List

    6月10日:登录功能的实现 第1组,郑超,90 第5组,张琳,90 ----------------------------- 6月11日上午:登录优化 第7组,张朋,90 6月11日下午:查询操作实 ...

  10. Buenos Aires和Rio de Janeiro怎么发音?

    Buenos Aires和Rio de Janeiro怎么发音?_百度知道     Buenos Aires和Rio de Janeiro怎么发音?    2009-09-25 08:58 zd029 ...