2013
11-09

# Drainage Ditches

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.

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.

For each case, output a single integer, the maximum rate at which water may emptied from the pond.

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10


50

//* @author:
import java.util.*;
public class Main implements Comparator< Integer> {
static class Edge{
int e,c;
Edge next;
Edge rev;
public void dec(int f){
c-=f;
rev.c+=f;
}
}

int n,m,src,tag;
final Edge[] g=new Edge[200];
final int[] h=new int[200];  final int[] ex=new int[200];
final Scanner sc=new Scanner(System.in);
final PriorityQueue< Integer> pq=new PriorityQueue< Integer>(200,this);

boolean init(){
if(!sc.hasNext()) return false;
m=sc.nextInt();
n=sc.nextInt();
src=0;   tag=n-1;
for(int i=0;i!=n;++i){
g[i]=null;
h[i]=0;
ex[i]=0;
}

h[src]=n;
for(int i=0;i!=m;++i){
int a=sc.nextInt()-1,b=sc.nextInt()-1,c=sc.nextInt();
e0.rev=e1;e1.rev=e0;
}    return true;
}

private Edge addEdge(int s, int e, int c) {
Edge n=new Edge();
n.c=c;
n.e=e;
n.next=g[s];
g[s]=n;
return n;
}

int work(){
for(Edge e=g[src];e!=null;e=e.next){
ex[e.e]+=e.c;
e.dec(e.c);
if(e.e!=src&&e.e!=tag){
}
}
while(pq.size()>0){
int v=pq.poll();
int exv=ex[v],hv=h[v];
for(Edge e=g[v]; exv>0&&e!=null; e=e.next){
if(e.c<=0 || hv !=h[e.e]+ 1 ) continue;
int f=Math.min( exv , e.c);
ex[e.e]+=f;
exv-=f;
e.dec(f);
if(ex[e.e]==f&&e.e!=tag&&e.e!=src){
}
}
if(exv>0){
for(Edge e=g[v];e!=null;e=e.next){
if(e.c > 0 && hv>h[e.e]){
hv=h[e.e];
}
}
++hv;
}
ex[v]=exv;
h[v]=hv;
}
return ex[tag];
}

void go(){     while(init()){
System.out.println( work());
}
}

public static void main(String[] args) {
new Main().go();
}

public int compare(Integer o1, Integer o2) {
return h[o2]-h[o1];
}
}

1. 你的理解应该是：即使主持人拿走一个箱子对结果没有影响。这样想，主持人拿走的箱子只是没有影响到你初始选择的那个箱子中有奖品的概率，但是改变了其余两个箱子的概率分布。由 1/3,1/3 变成了 0, 2/3

2. 其实国内大部分公司对算法都不够重视。特别是中小型公司老板根本都不懂技术，也不懂什么是算法，从而也不要求程序员懂什么算法，做程序从来不考虑性能问题，只要页面能显示出来就是好程序，这是国内的现状，很无奈。