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 region 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 shocould 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
12South.xiaolitang South.xiongdelong 2South.xiongdelong Zhuhai.liyuan 100South.xiongdelong South.xiaolitang
Sample output
2
Problem Source
Zsuacm team member
# Include <iostream> # include <map> # include <string> # include <cstring> using namespace STD; const int INF = 100000; // custom infinity int adj [205] [205]; // records the bool flag distance between two points [205]; // mark the processed vertex int dis [205]; // record the distance from each point to a certain point: int T, road, d, n; // t indicates the number of test data groups, and road indicates the number of data rows in each group, d indicates the distance between two points in the absence of data, and N records map the corresponding path numbers string start, send; // The two paths entered in each line int Dijkstra (int A, int B) // The Dijkstra method processes the shortest short circuit from A to B {memset (flag, 0, sizeof (FLAG); For (INT I = 0; I <n; I ++) dis [I] = (I = )? 0: INF); // The initialization distance from the start point is inffor (INT I = 0; I <n; I ++) {int min = inf, x =; /// X indicates the point closest to a (INT y = 0; y <n; y ++) {If (! Flag [y] & dis [y] <min) {min = dis [y]; X = y ;}} flag [x] = 1; // mark the nearest vertex. Select for (INT y = 0; y <n; y ++) from the unmarked vertex next time) dis [y] = (DIS [y]> dis [x] + adj [x] [Y]? Dis [x] + adj [x] [Y]: DIS [y]);} If (flag [B]) // If point A is connected to point B, return dis [B]; else return-1;} int main () {CIN> T; while (t --) {CIN> Road; For (INT I = 0; I <205; I ++) for (Int J = 0; j <205; j ++) adj [I] [J] = (I = J )? 0: INF); Map <string, int> mymap; // path corresponds to n = 0; // n records the corresponding path number for (INT I = 0; I <road; I ++) {// string end; CIN> Start> send> D; If (! Mymap. Count (start) mymap [start] = n ++; If (! Mymap. count (send) mymap [send] = n ++; adj [mymap [start] [mymap [send] = adj [mymap [send] [mymap [start] = D;} CIN >>> Start> send; if (START = Send) cout <0 <Endl; else if (! Mymap. Count (start) |! Mymap. count (send) cout <-1 <Endl; else cout <Dijkstra (mymap [start], mymap [send]) <Endl;} return 0 ;}