一、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. [置顶] 2013_CSUST暑假训练总结

    2013-7-19 shu 新生训练赛:母函数[转换成了背包做的] shuacm 题目:http://acm.hdu.edu.cn/diy/contest_show.php?cid=20083总结:h ...

  2. linux shell脚本: 自动监控网站状态并发送提醒邮件

    1.创建监控脚本:$ vi /alidata/shell/webcheck.sh #!/bin/sh weblist="/alidata/shell/weblist.txt" my ...

  3. 【python】-- 类的反射

    反射 反射我们以后会经常用到,这个东西实现了动态的装配,通过字符串来反射类中的属性和方法 一.反射函数 1.hasarttr(obj,name_str) 作用:判断一个对象obj中是否有对应的name ...

  4. Basis 基础

    [转自 http://www.cnblogs.com/elegantok/archive/2008/11/03/1325163.html] 1 Basis1. Unix操作 更改口令 passwd e ...

  5. (转)复习TCP/IP协议与Http协议的区别

    TPC/IP协议是传输层协议,主要解决数据如何在网络中传输,而HTTP是应用层协议,主要解决如何包装数据.关于TCP/IP和HTTP协议的关系,网络有一段比较容易理解的介绍:“我们在传输数据时,可以只 ...

  6. Linux基础系列:常用命令(6)_nfs服务与nginx服务

    NFS介绍: NFS 是Network File System的缩写,即网络文件系统.一种使用于分散式文件系统的协定,由Sun公司开发,于1984年向外公布.功能是通过网络让不同的机器.不同的操作系统 ...

  7. Java多线程系列 JUC线程池06 线程池原理解析(五)

    ScheduledThreadPoolExecutor解析 ScheduledThreadPoolExecutor适用于延时执行,或者周期性执行的任务调度,ScheduledThreadPoolExe ...

  8. 20145229吴姗珊 《Java程序设计》第9周总结

    20145229吴姗珊 <Java程序设计>第9周总结 教材学习内容总结 第十六章 整合数据库 JDBC入门 数据库本身是个独立运行的应用程序 撰写应用程序是利用通信协议对数据库进行指令交 ...

  9. 吴恩达机器学习笔记(四) —— BP神经网络

    主要内容: 一.模型简介 二.一些变量所代表的含义 三.代价函数 四.Forward Propagation 五.Back Propagation 六.算法流程 待解决问题: 视频中通过指出:当特征变 ...

  10. 机器学习(二十三)— L0、L1、L2正则化区别

    1.概念  L0正则化的值是模型参数中非零参数的个数. L1正则化表示各个参数绝对值之和. L2正则化标识各个参数的平方的和的开方值. 2.问题  1)实现参数的稀疏有什么好处吗? 一个好处是可以简化 ...