首页 > 专题系列 > Java解POJ > POJ 2263 Heavy Cargo [解题报告] Java
2013
11-11

POJ 2263 Heavy Cargo [解题报告] Java

Heavy Cargo

问题描述 :

Big Johnsson Trucks Inc. is a company specialized in manufacturing big trucks. Their latest model, the Godzilla V12, is so big that the amount of cargo you can transport with it is never limited by the truck itself. It is only limited by the weight restrictions that apply for the roads along the path you want to drive.

Given start and destination city, your job is to determine the maximum load of the Godzilla V12 so that there still exists a path between the two specified cities.

输入:

The input will contain one or more test cases. The first line of each test case will contain two integers: the number of cities n (2<=n<=200) and the number of road segments r (1<=r<=19900) making up the street network.

Then r lines will follow, each one describing one road segment by naming the two cities connected by the segment and giving the weight limit for trucks that use this segment. Names are not longer than 30 characters and do not contain white-space characters. Weight limits are integers in the range 0 – 10000. Roads can always be travelled in both directions.

The last line of the test case contains two city names: start and destination.

Input will be terminated by two values of 0 for n and r.

输出:

For each test case, print three lines:
  • a line saying “Scenario #x” where x is the number of the test case
  • a line saying “y tons” where y is the maximum possible load
  • a blank line

样例输入:

4 3
Karlsruhe Stuttgart 100
Stuttgart Ulm 80
Ulm Muenchen 120
Karlsruhe Muenchen
5 5
Karlsruhe Stuttgart 100
Stuttgart Ulm 80
Ulm Muenchen 120
Karlsruhe Hamburg 220
Hamburg Muenchen 170
Muenchen Karlsruhe
0 0

样例输出:

Scenario #1
80 tons
 
Scenario #2
170 tons

解题代码:

//* @author: [email protected]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main
{
 static int[][] w=new int[256][256];
 public static void main(String[] args) throws NumberFormatException, IOException
 {
  InputStreamReader is=new InputStreamReader(System.in);
  BufferedReader in=new BufferedReader(is);
  int cnt=0;
  while(true)
  {
   cnt++;
   String[] ss=in.readLine().split(" ");
   int n=Integer.parseInt(ss[0]);
   int m=Integer.parseInt(ss[1]);
   if(n==0&&m==0) break;
   for(int i=0;i< n;i++)
    for(int j=0;j< n;j++)
	w[i][j]=0;
   for(int i=0;i< n;i++)
    w[i][i]=1000000000;
   String[] name=new String[n+1];
   int len=0;
   for(int i=0;i< m;i++)
   {
	ss=in.readLine().split(" ");
	int a=-1;
	for(int j=0;j< len;j++)
	  if(ss[0].equals(name[j]))
	   {
	    a=j;
	    break;
	   }
	if(a==-1){
	   a=len;
	   name[len++]=ss[0];
	}
	int b=-1;
	for(int j=0;j< len;j++)
	  if(ss[1].equals(name[j]))
	  {
	    b=j;
	    break;
	   }
	if(b==-1){
	  b=len;
	  name[len++]=ss[1];
	}
	w[a][b]=w[b][a]=Integer.parseInt(ss[2]);
     }
     ss=in.readLine().split(" ");
     int str=-1,dis=-1;
     for(int i=0;i< n;i++)
      {
	if(ss[0].equals(name[i]))
		str=i;
	if(ss[1].equals(name[i]))
		dis=i;
      }
     for(int k=0;k< n;k++)
	for(int i=0;i< n;i++)
	 for(int j=0;j< n;j++)
	   w[i][j]=Math.max(w[i][j], Math.min(w[i][k], w[k][j]));
     System.out.println("Scenario #"+cnt);
     System.out.println(w[str][dis]+" tons");
     System.out.println();
    }
  }
}

  1. 嗯 分析得很到位,确实用模板编程能让面试官对你的印象更好。在设置辅助栈的时候可以这样:push时,比较要push的elem和辅助栈的栈顶,elem<=min.top(),则min.push(elem).否则只要push(elem)就好。在pop的时候,比较stack.top()与min.top(),if(stack.top()<=min.top()),则{stack.pop();min.pop();},否则{stack.pop();}.

  2. L(X [0 .. M-1],Y [0 .. N-1])= 1 + L(X [0 .. M-2],Y [0 .. N-1])这个地方也也有笔误
    应改为L(X [0 .. M-1],Y [0 .. N-1])= 1 + L(X [0 .. M-2],Y [0 .. N-2])