Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 71559   Accepted: 27846

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


最大流裸题
Dinic算法的基本思路:
  1. 根据残量网络计算层次图。
  2. 在层次图中使用DFS进行增广直到不存在增广路
  3. 重复以上步骤直到无法增广
层次图
阻塞流 不考虑反向边时的极大流 
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=,M=,INF=1e9;
int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,m,u,v,c;
struct edge{
int v,ne,f,c;
}e[M<<];
int h[N],cnt=;
void ins(int u,int v,int c){
cnt++;
e[cnt].v=v;e[cnt].c=c;e[cnt].f=;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].c=;e[cnt].f=;e[cnt].ne=h[v];h[v]=cnt;
}
int cur[N],d[N],s,t;
int vis[N],q[N],head=,tail=; int stop=;
bool bfs(){
memset(vis,,sizeof(vis));
memset(d,,sizeof(d));
head=tail=;
q[tail++]=s;d[s]=;vis[s]=;
while(head!=tail){
int u=q[head++];
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(!vis[v]&&e[i].f<e[i].c){
q[tail++]=v;vis[v]=;
d[v]=d[u]+;
if(v==t) return ;
}
}
}
return ;
} int dfs(int u,int a){
if(u==t||a==) return a;
int flow=,f;
for(int &i=cur[u];i;i=e[i].ne){
int v=e[i].v;
if(d[v]==d[u]+&&(f=dfs(v,min(a,e[i].c-e[i].f)))>){
flow+=f;
e[i].f+=f;
e[((i-)^)+].f-=f;
a-=f;
if(a==) break;
}
}
return flow;
}
int dinic(){
int flow=;
while(bfs()){
for(int i=s;i<=t;i++) cur[i]=h[i];
flow+=dfs(s,INF);
}
return flow;
}
int main(){
while(scanf("%d%d",&m,&n)!=EOF){
cnt=;
memset(h,,sizeof(h));
for(int i=;i<=m;i++){
u=read();v=read();c=read();
ins(u,v,c);
}
s=;t=n;
printf("%d\n",dinic());
}
}
 

POJ1273Drainage Ditches[最大流]的更多相关文章

  1. POJ-1273-Drainage Ditches(网络流之最大流)

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

  2. poj1273--Drainage Ditches(最大流Edmond-Karp算法 邻接表实现)

    最大流模板题 大部分Edmond-Karp算法代码都是邻接矩阵实现,试着改成了邻接表. #include <iostream> #include <cstdio> #inclu ...

  3. 图论-网络流-最大流--POJ1273Drainage Ditches(Dinic)

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 91585   Accepted: 3549 ...

  4. POJ-1273Drainage Ditches(网络流入门题,最大流)

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

  5. 【最大流Dinic模板】HDU1532&POJ1273-Drainage Ditches(16/3/6更正)

    #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...

  6. Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )

    题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... #include<cstdio> #include ...

  7. poj 1273 Drainage Ditches 最大流入门题

    题目链接:http://poj.org/problem?id=1273 Every time it rains on Farmer John's fields, a pond forms over B ...

  8. POJ 1273 || HDU 1532 Drainage Ditches (最大流模型)

    Drainage DitchesHal Burch Time Limit 1000 ms Memory Limit 65536 kb description Every time it rains o ...

  9. POJ-1273-Drainage Ditches 朴素增广路

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 70588   Accepted: 2743 ...

随机推荐

  1. ASP.NET MVC 了解FileResult的本质

    FileResult是一个基于文件的ActionResult,利用FileResult我们可以很容易地将从某个物理文件的内容响应给客户端.ASP.NET MVC定义了三个具体的FileResult,分 ...

  2. 基于小脚丫的ADC081S101 电压采集595数码管显示

    RTL结构图 采集模块运用SPI 通讯 MISO方式收集数据 module ad_collect(input sddata,input rst_n,output reg cs,output reg s ...

  3. C++_系列自学课程_第_6_课_bitset集_《C++ Primer 第四版》

    在C语言中要对一个整数的某一个位进行操作需要用到很多的技巧.这种情况在C++里面通过标准库提供的一个抽象数据类型 bitset得到了改善. 一.标准库bitset类型 1.bitset的作用 bits ...

  4. JAVA 异常处理机制

    主要讲述几点: 一.异常的简介 二.异常处理流程 三.运行时异常和非运行时异常 四.throws和throw关键字 一.异常简介 异常处理是在程序运行之中出现的情况,例如除数为零.异常类(Except ...

  5. 使用PowerDesigner设计建造MySQL数据库

    使用PowerDesigner设计建造MySQL数据库 一.使用PowerDesigner制作建库脚本 1.设计CDM(Conceptual Data Model) 2.选择 Tools -> ...

  6. Hibernate总结(一)

    Hibernate为了提高性能,提供了缓存与快照机制. 它的缓存分为一级缓存与二级缓存. Hibernate一级缓存:当一个事务中执行一次Sql语句时,就将返回的结果存储在Session中的Map集合 ...

  7. POI导入导出

    一.使用POI导出Execl表格 需要的jar包 package cn.yxj.poi; import java.io.FileOutputStream; import java.util.Date; ...

  8. 使用MyEclipse中servlet对SQL Server 2008的CRUD

    1.在MyEclipse下建立Web Project,找到根目录建立Database文件夹和Doc文件夹,Database用于保存数据库信息,Doc用于保存数据库表信息. 2.打开SQL Server ...

  9. SQL Server 2012提供的OFFSET/FETCH NEXT与Row_Number()对比测试(转)

    原文地址:http://www.cnblogs.com/downmoon/archive/2012/04/19/2456451.html 在<SQL Server 2012服务端使用OFFSET ...

  10. eclipse Swt编程—窗口小部件widget

    1.标签Label // 标签(Label类)组件是SWT中最简单的组件.Label类的构造方法和格式如下: // Label(Composite parent,