Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 78671   Accepted: 30687

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

题解

网络流模板题。。。

dinic求最大流

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define inf 1<<30
using namespace std; struct edge{
int to,ne,cap;
}e[]; int n,m,s,t,a,b,c,ans,ecnt;
int head[],layer[];
queue<int> q; void add(int x,int y,int z)
{
e[++ecnt].to=y;e[ecnt].cap=z;e[ecnt].ne=head[x];head[x]=ecnt;
e[++ecnt].to=x;e[ecnt].cap=;e[ecnt].ne=head[y];head[y]=ecnt;
} bool bfs()
{
//while(!q.empty())q.pop();
q.push(s);
for(int i=;i<=m;++i)layer[i]=;
layer[s]=;
while(!q.empty())
{
int d=q.front();
q.pop();
for(int i=head[d];i;i=e[i].ne)
{
int dd=e[i].to;
if(e[i].cap>&&layer[dd]==)
{
layer[dd]=layer[d]+;
q.push(dd);
}
}
}
return layer[t];
} int dfs(int x,int val)
{
if(val==||x==t)return val;
int ret=;
for(int i=head[x];i;i=e[i].ne)
{
int dd=e[i].to;
if(e[i].cap>&&layer[dd]==layer[x]+)
{
int tmp=dfs(dd,min(val,e[i].cap));
ret+=tmp;
val-=tmp;
e[i].cap-=tmp;
e[(i-)^+].cap+=tmp;
}
}
return ret;
} void dinic()
{
while(bfs())
{
ans+=dfs(s,inf);
}
} int main()
{
while(~scanf("%d%d",&n,&m))
{
ecnt=;ans=;
memset(head,,sizeof(head));
for(int i=;i<=n;++i)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
s=;t=m;
dinic();
printf("%d\n",ans);
}
}

【网络流】POJ1273 Drainage Ditches的更多相关文章

  1. poj1273 Drainage Ditches Dinic最大流

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

  2. POJ1273 Drainage Ditches (网络流)

                                                             Drainage Ditches Time Limit: 1000MS   Memor ...

  3. poj1273 Drainage Ditches

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 68414   Accepted: 2648 ...

  4. POJ-1273 Drainage Ditches 最大流Dinic

    Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65146 Accepted: 25112 De ...

  5. 2018.07.06 POJ1273 Drainage Ditches(最大流)

    Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Description Every time it rains on Farmer J ...

  6. POJ1273:Drainage Ditches(最大流入门 EK,dinic算法)

    http://poj.org/problem?id=1273 Description Every time it rains on Farmer John's fields, a pond forms ...

  7. poj-1273 Drainage Ditches(最大流基础题)

    题目链接: Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 67475   Accepted ...

  8. 网络流入门 Drainage Ditches

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) ...

  9. [Poj1273]Drainage Ditches(网络流)

    Description 给图,求最大流 最大流模板题,这里用dinic Code #include <cstdio> #include <cstring> #include & ...

随机推荐

  1. 5个步骤,将 storyboard 从 iphone 版转变为 ipad 版

    1.将 iPhone 版的 Storyboard 复制为 iPad 的,比如 Main_iPad.storyboard 2.用文本编辑器(不要用 Xcode)打开 Main_iPad.storyboa ...

  2. hdu 4869 Turn the pokers (思维)

    Turn the pokers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. 浅谈MySQL中的查询优化

    mysql的性能优化包罗甚广: 索引优化,查询优化,查询缓存,服务器设置优化,操作系统和硬件优化,应用层面优化(web服务器,缓存)等等.这里的记录的优化技巧更适用于开发人员,都是从网络上收集和自己整 ...

  4. java语言实现树

    首先用Node类定义一个节点,用来存储每个节点的内容: public class Node { // 关键字 private int keyData; // 其他数据 private int othe ...

  5. ligerUI---ligerGrid分页排序的使用(从后台获取数据显示)

    写在前面: 最近项目的前框框架用的是ligerUI,里面用到了ligerGrid表格,下面就来说说从后台获取数据并在前台页面进行完美展示.啊哈哈哈..(天啦,坐我旁边的丽姐貌似炒股 一个月可以搞几十万 ...

  6. 王者齐聚!Unite 2017 Shanghai 日程讲师全揭晓

    汇聚了来自全球的 Unity开发者.发行商.培训家及爱好者的 Unite 2017 Shanghai 即将于于 5 月 11 日-13日在上海·国际会议中心隆重举行.Unite 大会是由 Unity ...

  7. 【Linux】Linux学习笔记(完结)

    前言 在工作中发现Linux系统的重要性,于是计划重温下Linux,顺便记录笔记方便之后查阅. 磁盘分区 在Linux系统中,每个设备都被当成一个文件来对待:如IDE接口的硬盘文件名为/dev/hd[ ...

  8. 栈和队列数据结构的相互实现[LeetCode]

    栈是先进后出,队列是先进后出,这里讨论一下两种数据结构之间的相互实现. 一.用两个栈实现队列 我们用一个栈来实现队列的进队操作(栈A),用另一个栈来实现队列的出队操作(栈B). 1.入队列: 把元素放 ...

  9. Python图片爬虫

    1.今天给大家介绍自己写的一个图片爬虫,说白了就是从网页自动上下载需要的图片 2.首先选取目标为:http://www.zhangzishi.cc/涨姿势这个网站如下图,我们的目标就是爬取该网站福利社 ...

  10. jquery学习总结(超级详细)

    本文仅针对jquery的部分知识点做总结,更为全面的可以去官网看中文文档.可以更为详细的了解jquery及其特性.       window.onload $(document).ready() 执行 ...