2013
11-11

# 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]/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */
import java.io.IOException;
import java.util.*;
public class Main
{
static int[][] w=new int[256][256];
public static void main(String[] args) throws NumberFormatException, IOException
{
int cnt=0;
while(true)
{
cnt++;
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++)
{
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]);
}
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]）