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. Android设计模式(九)--外观模式

    问题:在Android中,Apk能够有微信,QQ为代表的插件式安装更新功能: 那么问题来了,主系统(姑且这么说)调用插件式安装的子系统.由子系统提供对外的訪问.属不属于一种外观模式呢? 先说设计模式: ...

  2. java.lang.IllegalStateException: attempt to re-open an already-closed object

    attempt to re-open an already-closed object 字面理解,试图再次打开已经关闭的对象.这是我在操作sqlited的时候出现的错误, 我在一个activity里面 ...

  3. 第03章-VTK系统概述(1)

    [译者:这个系列教程是以Kitware公司出版的<VTK User's Guide -11th edition>一书作的中文翻译(出版时间2010年,ISBN: 978-1-930934- ...

  4. CI学习 CCNET Config 第一天

    CCNet的整体结构就是一个Xml文档,根元素就是cruisecontrol,具体的代码块如下所示: <cruisecontrol xmlns:cb="urn:ccnet.config ...

  5. 「mysql优化专题」高可用性、负载均衡的mysql集群解决方案(12)

    一.为什么需要mysql集群? 一个庞大的分布式系统的性能瓶颈中,最脆弱的就是连接.连接有两个,一个是客户端与后端的连接,另一个是后端与数据库的连接.简单如图下两个蓝色框框(其实,这张图是我在悟空问答 ...

  6. 【java API基本实现】LinkedList

    LinkedList: package com.tn.arraylist; public class LinkedList { Node head=null; Node tail=null; int ...

  7. 【HTML5】Canvas

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. Solr集群搭建详细教程(二)

    注:欢迎大家转载,非商业用途请在醒目位置注明本文链接和作者名dijia478,商业用途请联系本人dijia478@163.com. 之前步骤:Solr集群搭建详细教程(一) 三.solr集群搭建 注意 ...

  9. XMPP协议的基本理解

    即时通讯技术简介 即时通讯技术(IM)支持用户在线实时交谈.如果要发送一条信息,用户需要打开一个小窗口,以便让用户及其朋友在其中输入信息并让交谈双方都看到交谈的内容.大多数常用的即时通讯发送程序都会提 ...

  10. python学习日记:day11----装饰器进阶

    1.wraps from functools import wraps def wrapper(func): #func = holiday @wraps(func)#输出holiday的函数名 de ...