链接:

http://poj.org/problem?id=1273

Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 63763   Accepted: 24613

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

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
#define N 210
#define INF 0x3f3f3f3f int n, m, G[N][N], pre[N]; bool BFS(int Start, int End)
{
int p; queue<int>Q;
Q.push(Start); memset(pre, , sizeof(pre)); while(Q.size())
{
p = Q.front(), Q.pop(); if(p==End) return true; for(int i=; i<=End; i++)
{
if(!pre[i] && G[p][i])
{
pre[i] = p;
Q.push(i);
}
}
}
return false;
} int EM(int Start, int End)
{
int Max=;
while(BFS(Start, End)==true)
{
int Min=INF; for(int i=End; i!=Start; i=pre[i])
Min = min(Min, G[pre[i]][i]);
for(int i=End; i!=Start; i=pre[i])
{
G[pre[i]][i] -= Min;
G[i][pre[i]] += Min;
} Max += Min;
}
return Max;
} int main()
{
while(scanf("%d%d", &n, &m)!=EOF)
{
int i, u, v, p; memset(G, , sizeof(G));
for(i=; i<n; i++)
{
scanf("%d%d%d", &u, &v, &p);
G[u][v] += p;
} printf("%d\n", EM(, m));
}
return ;
}

最新文章

  1. mvc mvp mvvm模式的区别
  2. sys.dm_os_waiting_tasks 引发的疑问(下)
  3. javaSE基础02
  4. HTML5体验改进的14条军规
  5. WinForm开发之取送货管理1
  6. {CSDN}{英雄会}{反相互}
  7. 解决TCP网络传输粘包问题
  8. 关于NSDate和NSDateFormatter的几个常用方法
  9. Spring 中 Xml配置文件属性的说明
  10. 9.配置postfix空客户端
  11. [LeetCode OJ] Symmetric Tree
  12. 自动化单元测试工具 EvoSuite 的简单使用
  13. Android Afinal框架学习(二) FinalActivity 一个IOC框架
  14. Django Web开发【3】创建网络收藏夹
  15. 一天搞定CSS:表单(form)--20
  16. Problem W
  17. python数据结构与算法第十二天【快速排序】
  18. (转)HTTPS到底是个啥玩意儿?
  19. sql 分隔字符串函数
  20. kaldi脚本注释二

热门文章

  1. How to Pronounce WH Words — what, why, which
  2. Java Thread.interrupt interrupted
  3. 第八章&#160;高级搜索树 (a3)伸展树:算法实现
  4. Python oct() 函数
  5. 123. Best Time to Buy and Sell Stock III (Array; DP)
  6. Linux找不到动态库
  7. tcp连接需要注意的问题
  8. php多进程 防止出现僵尸进程
  9. mysql优化概述3
  10. spring框架之数组,集合(List,Set,Map),Properties等的注入