12:Heavy Cargo

Description
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.


Input
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.


Output
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

Sample Input

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

Sample Output

Scenario #1

80 tons


Scenario #2

170 tons


编的不太好,不过思路就是广度优先搜索,求两点间所有路径,每个点保持一个经过他的路径的最大值,每次路过一个点之前,先将进入他的边上的另一个顶点保持的最大值和这条边上的值进行比较,选择较小的那个,与该点保持的最大值进行比较,较大者赋给该点作为该点保持的最大值。

走到最后,到终点时,终点保持的最大值就是该题的结果

package OJ;
import java.util.*;

public class P12_temp {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		class Graph{
			
//			class Vertex {
//				int id;
//				boolean isVisited;
//				
//				public Vertex(int id){
//					this.id = id;
//					isVisited = false;
//				}
//			}
			
			ArrayList<Integer> vertexList = new ArrayList<Integer>();//顶点数组
			int[][] adjMat;//邻接矩阵
			int vertexNum; //顶点数
			ArrayList<Integer> min = new ArrayList<Integer>();
			int start;
			int end;
//			int Min = 0;
			
			public Graph(int n){
				adjMat = new int[n][n];
				vertexNum = n;
			}
			
			public void addVertex(int id){
				vertexList.add(id);
			}
			
			public void addEdge(int i, int j, int heavy){
				adjMat[i][j] = heavy;
				adjMat[j][i] = heavy;
			}
			
			public void add(int start, int end){
				this.start = start;
				this.end = end;
			}
			
			public void bfs(int id, ArrayList<Integer> vertexList, int sum){				
//				int temp = sum;
				if(id == end) {
					if(sum != 0)
						min.add(sum);
				}
				else{
					ArrayList<Integer> vertexListcopy = (ArrayList<Integer>) vertexList.clone();
					vertexListcopy.remove(Integer.valueOf(id));
					for(int v : vertexListcopy){
						int temp = sum;
						if(sum == 0 && adjMat[id][v] != 0){
							temp = adjMat[id][v];
//							bfs(v, vertexListcopy, sum);
						}
						else	if(adjMat[id][v] < sum && adjMat[id][v] != 0) {
							temp = adjMat[id][v];
//								bfs(v, vertexListcopy, sum);
						}
						else if(adjMat[id][v] == 0)
							continue;
						bfs(v, vertexListcopy, temp);
					}
				}				
			}
			
			public int Max(){
				int Min = 0;
				for(int i : min){
					if(i > Min)
						Min = i;
				}
				return Min;
			}
			
		}
		
		
		Scanner in = new Scanner(System.in);
		ArrayList<Integer> results = new ArrayList<Integer>();
		
		while(true){
			HashMap<String, Integer> name_id = new HashMap<String, Integer>();
            int start = 0;
            int end = 0;
			int n =in.nextInt();
			int r = in.nextInt();
			int id = -1;
			Graph g = new Graph(n);
			
			if(n == 0 && r == 0) //结束程序
				break;
			else{
				for(int i=0; i<r; i++){
					int id1 = 0;
					String city1 = in.next();
					if(!name_id.containsKey(city1)){
						id++;  id1 = id;
						name_id.put(city1, id);
						g.addVertex(id);
					}
					else
						id1 = name_id.get(city1);					
					int id2 = 0;
					String city2 = in.next();
					if(!name_id.containsKey(city2)){
						id++;  id2 = id;
						name_id.put(city2, id);
						g.addVertex(id);
					}
					else
						id2 = name_id.get(city2);
					g.addEdge(id1, id2, in.nextInt());
				}
				start = name_id.get(in.next());
				end = name_id.get(in.next());
				if(start > end){
					int temp = start;
					start = end;
					end = temp;
				}
				g.add(start, end);
				g.bfs(start, g.vertexList, 0);
//				System.out.println(g.min);
				int result = g.Max();
//	            System.out.println(result);
				results.add(result);
			}
		}
		for(int i=1; i <=results.size(); i++){
			System.out.println("Scenario #" + i);
			System.out.println(results.get(i-1) +" tons");
			System.out.println();
		}
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值