首页 > 专题系列 > Java解POJ > POJ 1273 Drainage Ditches [解题报告] Java
2013
11-09

POJ 1273 Drainage Ditches [解题报告] Java

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();
   Edge e0=addEdge(a, b, c),e1=addEdge(b,a,0);
   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){
     pq.add(e.e);
   }
  }
  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){
     pq.add(e.e);
    }
   }
   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;
    pq.add(v);
   }
   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. 其实国内大部分公司对算法都不够重视。特别是中小型公司老板根本都不懂技术,也不懂什么是算法,从而也不要求程序员懂什么算法,做程序从来不考虑性能问题,只要页面能显示出来就是好程序,这是国内的现状,很无奈。

  3. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。