Drainage Ditches

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 70451   Accepted: 27391

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

最大流入门题,裸的Edmonds-Karp。
 //2016.9.22
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define N 205 using namespace std; const int inf = 0x3f3f3f3f; struct Edge
{
int from, to , cap, flow;//分别为边的两个端点、容量、边上的流
Edge(int u, int v, int c, int f):from(u), to(v), cap(c), flow(f){}
}; struct EdmondsKarp//Edmonds-Karp算法
{
int n, m;//结点编号0——n-1
vector<Edge> edges;//存边与反向弧
vector<int> G[N];//邻接表,G[i][j]表示结点i的第j条边在数组edges中的序号
int a[N];//当起点到i的可改进量
int p[N];//最短路树上p的入弧编号 void init(int n)//初始化
{
for(int i = ; i < n; i++)G[i].clear();
edges.clear();
} void addEdge(int from, int to, int cap)//添加边
{
edges.push_back(Edge(from, to, cap, ));
edges.push_back(Edge(to, from, , ));//反向弧
m = edges.size();
G[from].push_back(m-);//加入编号
G[to].push_back(m-);
} int maxFlow(int s, int t)//最大流,s为源点,t为汇点
{
int flow = ;
while()
{
memset(a, , sizeof(a));
queue<int> Q;
Q.push(s);
a[s] = inf;
while(!Q.empty())
{
int x = Q.front(); Q.pop();
for(int i = ; i < G[x].size(); i++)
{
Edge& e = edges[G[x][i]];
if(!a[e.to] && e.cap>e.flow)
{
p[e.to] = G[x][i];
a[e.to] = min(a[x], e.cap-e.flow);
Q.push(e.to);
}
}
if(a[t])break;
}
if(!a[t])break;//残余网络中不存在增广路,当前流是最大流
for(int u = t; u != s; u = edges[p[u]].from)//找到一条增广路
{
edges[p[u]].flow += a[t];
edges[p[u]^].flow -= a[t];//p[u]^1为p[u]的反向边
}
flow += a[t];
}
return flow;
}
}; int main()
{
int n, m;
EdmondsKarp e;
while(scanf("%d%d", &m, &n)!=EOF)
{
int u, v, c;
e.init(n);
for(int i = ; i < m; i++)
{
scanf("%d%d%d", &u, &v, &c);
u--, v--;
e.addEdge(u, v, c);
}
printf("%d\n", e.maxFlow(, n-));
} return ;
}

POJ1273(最大流)的更多相关文章

  1. POJ1273 最大流模板

    之前自己写的,以后当一个模板的基础吧,不管是最大流,最小割,二分图匹配 下面是POJ1273的一个裸题.. 1 #include <iostream> 2 #include <cst ...

  2. POJ1273 最大流 EK算法

    套了个EK的模板 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdi ...

  3. POJ1273(最大流入门)

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 70333   Accepted: 2733 ...

  4. hdu1532&&poj1273 最大流

    Dinic算法: 层次图:根据源点到该点的距离建图,这里设相邻的都差1. (下面部分转) 在这幅图中我们首先要增广1->2->4->6,这时可以获得一个容量为2的流,但是如果不建立4 ...

  5. poj1273最大流初破

    第一次网络流,学了一天的DINIC算法(个人比较愚),切了这个入门题,开始的时候怎么调连 测试都过不了,后来发现犯了一个低级错误!把判断条件放在for(:)!里面和放在for下面大大 不同啊!里面的话 ...

  6. poj2月下旬题解

    poj2388 水题 poj1273 最大流初步 poj2456 简单的二分答案 poj2309 论lowbit的重要性 poj1734 floyd求最小环 poj1001 细节题 poj2184 0 ...

  7. poj1273 Drainage Ditches Dinic最大流

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 76000   Accepted: 2953 ...

  8. poj1273 Drainage Ditches (最大流板子

    网络流一直没学,来学一波网络流. https://vjudge.net/problem/POJ-1273 题意:给定点数,边数,源点,汇点,每条边容量,求最大流. 解法:EK或dinic. EK:每次 ...

  9. 经典的最大流题POJ1273(网络流裸题)

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

随机推荐

  1. iOS之NSDictionary和NSArray以及NSMutableDictionary和NSMutableArray:将不再是问题

    字典的key就相当于数组的下标,怎样操作数组你就学会了怎样才做字典:来感受一把 需要注意的是读取文件的类型要集合.plist文件的rooty:否则将不能读入文件 // 1.全国省市:得到的是省和市 / ...

  2. php中print_r 和var_dump 打印变量的区别。

    <?php $arr = array(true); var_dump($arr); echo "<br/>"; print_r($arr); 结果如下: 说明 p ...

  3. Cantor表 1083

    题目描述 Description 现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的.他是用下面这一张表来证明这一命题的: 1/1 1/2 1/3 1/4 1/5 - 2/1 2/ ...

  4. [转]ASP.NET Core 1 Deploy to IIS

    本文转自: http://webmodelling.com/webbits/aspnet/aspnet-deploy-iis.aspx 15 Sep 2016. This tutorial will ...

  5. log4cxx第三篇----使用多个logger

    使用多个logger时,所有logger的配置写在一个配置文件里面 两个例子: 1 一个继承的例子(http://logging.apache.org/log4cxx/) // file com/fo ...

  6. ucos任务优先级从64到256,任务就绪表的改变

    Ucos在任务调度中经常使用的技术为任务就绪表,在之前的文章中使用的例子是低于64个优先级的任务就绪表查找方法,现在ucos将任务扩展到256优先级之后,任务就绪表的查找也做了一定的修改,今天来讲讲 ...

  7. HDU 3501 Calculation 2 ——Dirichlet积

    [题目分析] 卷积太有趣了. 最终得出结论,互质数和为n*phi(n)/2即可. 计算(n*(n+1)/2-n-n*phi(n)/2)%md,用反正法即可证明. [代码] #include <c ...

  8. Codeforces Round 212 Div 2 报告(以前没写完,现在也没心情补了,先就这样吧)

    A. Two Semiknights Meet 题目大意:有一个8x8的棋盘,上面放有两个骑士,骑士以“田字”的方式走.每个方格都被定义为good或者bad,问骑士能否在good的格子中相遇? 由于骑 ...

  9. C# Expression表达式笔记

    整理了一下表达式树的一些东西,入门足够了 先从ConstantExpression 开始一步一步的来吧  它表示具有常量值的表达式 我们选建一个控制台应用程序 ConstantExpression _ ...

  10. 使用ExecutorCompletionService 管理线程池处理任务的返回结果

    在我们日常使用线程池的时候,经常会有需要获得线程处理结果的时候.此时我们通常有两种做法. 1. 使用并发容器将callable.call() 的返回Future存储起来.然后使用一个消费者线程去遍历这 ...