一、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. 核函数 深度学习 统计学习 强化学习 神经网络 xx

  2. 【题解】P2161[SHOI2009]会场预约(set)

    [题解][P2161 SHOI2009]会场预约 题目很像[[题解]APIO2009]会议中心 \(set\)大法好啊! 然后我们有个小\(trick\)(炒鸡帅),就是如何优雅地判断线段交? str ...

  3. greenlet和gevent模块的区别?

    协程是一中多任务实现方式,它不需要多个进程或线程就可以实现多任务. yield能实现协程,不过实现过程不易于理解,greenlet是在这方面做了改进,通过switch. greenlet可以实现协程, ...

  4. Android N API预览

    Android N for Developers 重要的开发人员功能 多窗体支持 通知 JIT/AOT 编译 高速的应用安装路径 外出瞌睡模式 后台优化 Data Saver 高速设置图块 API 号 ...

  5. 寻找第K大 网易2016实习研发工程师编程题

    有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5,2,2] ...

  6. SVN代码merge

    如何merge代码?建议用命令搞merge,客户端图形界面不是很给力.SVN 1.5以上版本,可以使用SVN的自动合并:将主干合并到分支:进入分支目录,执行命令: svn merge http://s ...

  7. SpringBoot学习笔记(7):Druid使用心得

    SpringBoot学习笔记(7):Druid使用心得 快速开始 添加依赖 <dependency> <groupId>com.alibaba</groupId> ...

  8. ARM NEON 64bit 查找表替换

    没啥效果,如果表的长度在 64个uint8_t之类,应该可以提高查表速度,否则还是C来的快 #ifdef HAVE_NEON_AARCH64 void table_lookup_AArch64_neo ...

  9. Ansible Ad-Hoc命令集

    Ad-Hoc Ad-Hoc就是 “临时命令”, 从功能上讲 Ad-Hoc跟Ansible-playbook都差不多,Ansible提供了两种完成任务的方式: Ad-Hoc命令集与Ansible-pla ...

  10. linux用户管理与用户组的重要文件

    用户管理的2个重要文件:/etc/passwd和/etc/shadow. /etc/passwd文件里存放的是用户的信息,其中不包含密码:passwd文件中每一行代表一个用户,且每一行分为7个字段使用 ...