一、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. 为什么不写 @RequestParam 也能拿到参数?

    三种写法,test(String name), test(@RequestParam String name), test(@RequestParam("userName") St ...

  2. xpath取最后一个元素

    取xpath最后一个book元素 book[last()] 取xpath最后第二个book元素 book[last()-1]

  3. Yii2之事件处理

    通过事件(Event)处理,可以在某个特定时刻执行指定的代码,可以解耦代码,同时也增加了可维护性,通常,事件在客户端软件中比较好理解,比如onClick,onFocus,当点击按钮,获取到焦点时执行指 ...

  4. iOS 展示 gif

    gif 图 是多张依次有连续动作的图 顺时间展示的一种动态效果图 .   有的是均匀时间更换下一张  有的 则不是均匀时间变化 1. 那么 对于均匀 时间变化的gif图 比较适合 使用 iOS 系统自 ...

  5. c# 文件IO操作 StreamReader StreamWriter Split 使用

    StreamWriter(String,Boolean) 若要追加数据到该文件中,则为 true:若要覆盖该文件,则为 false. 如果指定的文件不存在,该参数无效,且构造函数将创建一个新文件. 例 ...

  6. DevExpress实用心得:XtraGridControl动态添加右键菜

    在使用GridControl的时候经常需要添加右键菜单. 一般的做法是自己创建菜单项,然后注册GridView的Mouse-Click事件,然后Show出定义好的菜单. 但是涉及到一些单击事件会收到编 ...

  7. oracle 序列 + 触发器 实现 ID自动增长

    1.创建序列 create sequence emp_sequence increment by ----每次增加几个 minvalue ----最小值为1 nomaxvalue----不限制最大值 ...

  8. 常见http返回状态码

    200:表示从客户端发来的请求在服务器端被正常处理了. 302:临时重定向,该状态码表示请求的资源已经被分配了新的URI,希望用户本次能够通过新的UIRI访问. 304:未修改,服务端资源未改变,可直 ...

  9. HTTP1.1与HTTP1.0

    本文转载自: http://www.cnblogs.com/shijingxiang/articles/4434643.html 1.可扩展性 a.在消息中增添版本号,用于兼容判断,版本号只能判断逐段 ...

  10. hd acm 1465

    问题:某人写了n封信和n个信封,如果所有的信都装错了信封.求所有的信都装错信封,共有多少种不同情况. 思路:由这道题引入错排公式:f(n)=(n-1)*[f(n-1)+f(n-2)]. 当N=1和2时 ...