sicily 1031 Campus

Description

At present, Zhongshan University has 4 campuses with a total area of 6.17 square kilometers sitting respectively on both sides of the Pearl River or facing the South China Sea. The Guangzhou South Campus covers an area of 1.17 square kilometers, the North Campus covers an area of 0.39 square kilometers, the Guangzhou East Campus has an area of 1.13 square kilometers and the Zhuhai Campus covers an area of 3.48 square kilometers. All campuses have exuberance of green trees, abundance of lawns and beautiful sceneries, and are ideal for molding the temperaments, studying and doing research.

Sometime, the professors and students have to go from one place to another place in one campus or between campuses. They want to find the shortest path between their source place S and target place T. Can you help them?

Input

The first line of the input is a positive integer C. C is the number of test cases followed. In each test case, the first line is a positive integer N (0<N<=100) that represents the number of roads. After that, N lines follow. The i-th(1<=i<=N) line contains two strings Si, Ti and one integer Di (0<=Di<=100). It means that there is a road whose length is Di between Si and Ti. Finally, there are two strings S and T, you have to find the shortest path between S and T. S, T, Si(1<=i<=N) and Ti(1<=i<=N) are all given in the following format: str_Campus.str_Place. str_Campus represents the name of the campus, and str_Place represents the place in str_Campus. str_Campus is "North", "South", "East" or "Zhuhai". str_Place is a string which has less than one hundred lowercase characters from "a-z". You can assume that there is at most one road directly between any two places.

Output

The output of the program should consist of C lines, one line for each test case. For each test case, the output is a single line containing one integer. If there is a path between S and T, output the length of the shortest path between them. Otherwise just output "-1" (without quotation mark). No redundant spaces are needed.

Sample Input

1
2
South.xiaolitang South.xiongdelong 2
South.xiongdelong Zhuhai.liyuan 100
South.xiongdelong South.xiaolitang

Sample Output

2

分析:

直接用Dijkstra算法,需要注意可能出现输入数据中两端点相等的情况,要加上特殊的判断(比较坑爹)。

代码:

 1 // Problem#: 1031
 2 // Submission#: 1789656
 3 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
 4 // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
 5 // All Copyright reserved by Informatic Lab of Sun Yat-sen University
 6 #include <iostream>
 7 #include <string>
 8 #include <cstring>
 9 #include <map>
10 using namespace std;
11 
12 #define MAX 210
13 #define INF 1000000
14 
15 int buffer[MAX][MAX];
16 int len[MAX];
17 bool judge[MAX];
18 
19 int dijkstra( int b, int e, int n ){
20     memset(judge,0,sizeof(judge));
21     for( int i=0 ; i<n ; i++ )
22         len[i] = INF;
23     len[b] = 0;
24     for( int i=0 ; i<n ; i++ ){
25         int min = INF;
26         int v = b;
27         for( int j=0 ; j<n ; j++ )
28             if( !judge[j] && len[j]<min ){
29                 min = len[j];
30                 v = j;
31             }
32         judge[v] = true;
33         for( int j=0 ; j<n ; j++ )
34             if( !judge[j] && len[v]+buffer[v][j]<len[j] )
35                 len[j] = len[v]+buffer[v][j];
36     }
37     if( judge[e]) return len[e];
38     else return -1;
39 }
40 
41 int main(){
42     int c,n,l;
43     string b,e;
44     cin >> c;
45     while( c-- ){
46         cin >> n;
47         map<string,int> sysu;
48         int num = 0;
49         for( int i=0 ; i<MAX ; i++ )
50             for( int j=0 ; j<MAX ; j++ )
51                 buffer[i][j] = (i==j?0:INF);
52         for( int i=0 ; i<n ; i++ ){
53             cin >> b >> e >> l;
54             if( !sysu.count(b) ) sysu[b] = num++;
55             if( !sysu.count(e) ) sysu[e] = num++;
56             buffer[sysu[b]][sysu[e]] = buffer[sysu[e]][sysu[b]] = l;
57         }
58         cin >> b >> e;
59         if( b==e ) cout << 0 << endl;
60         else if( !sysu.count(b) || !sysu.count(e) ) cout << -1 << endl;
61         else cout << dijkstra(sysu[b],sysu[e],num) << endl;
62     }
63     return 0;
64 }
原文地址:https://www.cnblogs.com/ciel/p/2876764.html