一、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.
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.
(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代码
二、题解
这道题是求最大流问题,解决的方法是 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());
}
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。