一、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.

二、题解

        这道题是求最大流问题,解决的方法是 Ford — Fulkerson
方法,这种方法有几种实现途径。其中的一种就是 Edmonds–Karp
算法。这种方法采用了广度优先搜索(BFS)来找增广路径,并找到该路径上的最小值。用来构建残余矩阵。重复这一过程直到找不到增广路径,最大流就是每次增加的值。这个算法比较复杂,涉及到比较多的知识。小弟也是参考了《算法导论》,最大流问题请参考网络最大流问题

三、java代码

package Map;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner; public class E_K {
static int N = 210;
static int INF = Integer.MAX_VALUE;
static int n;
static int m;
static int start;
static int end;
static int map[][]=new int[N][N];
static int path[]=new int [N];
static int flow[]=new int [N];
static Queue<Integer> q=new LinkedList<Integer>();
static int bfs(){
int i,t;
while(!q.isEmpty()) //每次找到一条路径,下一次调用时清空q。
q.poll();
for(i=0;i<N;i++){ //每次找到一条路径,下一次调用时清空path。
path[i]=-1;
}
path[start]=0;
flow[start]=INF;
q.add(start);
while(!q.isEmpty()){
t=q.poll();
if(t==end)
break;
for(i=1;i<=m;i++){
if(i!=start && path[i]==-1 && map[t][i]!=0){ //i部位start,因为start已经讨论完了。路径path中不存在该结点
flow[i]=Math.min(flow[t], map[t][i]); // map中存在这条路径。
q.add(i);
path[i]=t;
}
}
} if(path[end]==-1) //最后一个结点的值仍为-1,表示没有路径到这里,即没有增广路径。
return -1;
return flow[m]; //一次遍历之后的流量增量,是路径中的最小权值。
}
static int Edmonds_Karp(){
int max_flow=0,step,now,pre;
while((step=bfs())!=-1){ //找不到增路径时退出
max_flow+=step;
now=end;
while(now!=start){
pre=path[now];
map[pre][now]-=step; //更新正向边的实际容量
map[now][pre]+=step; //添加反向边
now=pre;
}
}
return max_flow;
}
/**
* @param args
*/ public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int i,j,u,v,cost;
while(sc.hasNext()){
n=sc.nextInt();
m=sc.nextInt();
for(i=0;i<N;i++){
for(j=0;j<N;j++)
map[i][j]=0;
}
for(i=0;i<n;i++){
u=sc.nextInt();
v=sc.nextInt();
cost=sc.nextInt();
map[u][v]+=cost; //not just only one input
}
start=1;
end=m;
System.out.println(Edmonds_Karp());
}
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

poj 1273 Drainage Ditches(最大流,E-K算法)的更多相关文章

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

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

  2. POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]

    题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...

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

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

  4. POJ 1273 Drainage Ditches 最大流

    这道题用dinic会超时 用E_K就没问题 注意输入数据有重边.POJ1273 dinic的复杂度为O(N*N*M)E_K的复杂度为O(N*M*M)对于这道题,复杂度是相同的. 然而dinic主要依靠 ...

  5. POJ 1273 Drainage Ditches | 最大流模板

    #include<cstdio> #include<algorithm> #include<cstring> #include<queue> #defi ...

  6. POJ 1273 Drainage Ditches(最大流Dinic 模板)

    #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n, ...

  7. poj 1273 Drainage Ditches(最大流)

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

  8. POJ 1273 Drainage Ditches (网络最大流)

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

  9. poj 1273 Drainage Ditches【最大流入门】

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63924   Accepted: 2467 ...

  10. POJ 1273 Drainage Ditches(网络流,最大流)

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

随机推荐

  1. iPhone快速获取UUID

    1.一张图解决不懂iPhone手机的小白获取UDID的方式

  2. Centos 6 安装 python2.7 和 pip

    一.安装 python2.7 [root@crazy-acong ~]# cd /data/tools/ [root@crazy-acong tools]# yum groupinstall &quo ...

  3. ExtJS4.2.1与Spring MVC实现Session超时控制

    假设你的项目使用ExtJS作为表现层.你会发现,SESSION超时控制将是一个问题. 本文将就自己的经验.来解决这一问题.当然,解决这个问题并不是仅仅有一种方法,我仅仅是提出我的方法. 首先.做超时控 ...

  4. go语言之并发编程 channel(1)

    单向channel: 单向通道可分为发送通道和接收通道.但是无论哪一种单向通道,都不应该出现在变量的声明中,假如初始化了这样一个变量 var uselessChan chan <- int =m ...

  5. linux c编程:标准IO库

    前面介绍对文件进行操作的时候,使用的是open,read,write函数.这一章将要介绍基于流的文件操作方法:fopen,fread,fwrite.这两种方式的区别是什么呢.1种是缓冲文件系统,一种是 ...

  6. hdu 3718 Different Division

    Different Division Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  7. memcached 不同客户端的问题

    摘要: memcached-java客户端调用get方法获取数据失败 主要演示一下在memcached服务器端set数据之后,在客户端调用java api获取数据.不过此过程如果不慎会读取数据失败. ...

  8. Python基础(1)_python介绍、简单运算符

    Python执行一个程序分为三个阶段 阶段一:先启动python解释器 阶段二:python解释器把硬盘中的文件读入到内存中 阶段三:python解释器解释执行刚刚读入内存的代码 二.编程语言的分类: ...

  9. 自定义jsonp请求数据

    整理代码的时候发现一个以前写的实现jsonp请求方法,放在这里分享一下~ 原理:通过js新建script dom对象,利用src携带参数和callback方法,将数据发送至后端,需要后端配合将数据放在 ...

  10. 为jquery添加扩展标准思路

    jquery扩展分为对象扩展和jquery本身类扩展: 对象扩展: (function($){ $.fn.abc = function(){ console.log($(this).get(0)); ...