您的位置:首页 > 其它

1031. Campus

2015-10-02 13:53 288 查看

                                                          


1031. Campus

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

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


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

Sample Output


2



#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;

const int MAX = 1000000;
const int MA = 205;

int dis[MA];
int n; //结点数量
typedef pair<int, int> pairs;

struct Edge {
int x;  //起点
int y; //终点
int distance;  //起点与终点之间的距离
Edge(int u, int v, int w) {
x = u;
y = v;
distance = w;
}
};

int dijkstra(int st, int ed, vector<Edge> G[]) {
priority_queue<pairs> que; //定义一个优先队列
for (int i = 0; i < n; i++) {
dis[i] = ((i == st)?0:MAX);
}
que.push(make_pair(dis[st], st));

while (!que.empty()) {
pairs top = que.top();
que.pop();
int x = top.second;
if (top.first != dis[x]) continue;
int a = G[x].size();
for (int j = 0; j < a; j++) {
int yy = G[x][j].y;
int dist = G[x][j].distance;
if (dis[yy] > dis[x]+dist) {
dis[yy] = dis[x]+dist;
que.push(make_pair(dis[yy], yy));
}
}
}
if (dis[ed] == MAX) return -1;
else return dis[ed];
}

int main() {
int cases;
cin >> cases;
int lines;
string start, end;
int d;
while (cases--) {
cin >> lines;
vector<Edge> edge[MA];
map<string, int> maps;
n = 0;
for (int i = 0; i < lines; i++) {
cin >> start >> end >> d;
if (!maps.count(start))
maps.insert(make_pair(start, n++));
if (!maps.count(end))
maps.insert(make_pair(end, n++));
Edge e1(maps[start], maps[end], d);
Edge e2(maps[end], maps[start], d);
edge[maps[start]].push_back(e1);
edge[maps[end]].push_back(e2);
}

string st, ed;
cin >> st >> ed;
if (st == ed) cout << 0 << endl;
else if (!maps.count(st) || !maps.count(ed)) cout << -1 << endl;
else cout << dijkstra(maps[st], maps[ed], edge) << endl;
}
return 0;
}





                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: