[sicily online]1031. Campus

來源:互聯網
上載者:User
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

題目分析:

最短路徑問題,且沒有負值,可以用Dijkstra,且是無向圖的。我是用map來儲存圖(相當於鄰接表)的方法。用棧來遍曆。有一點需要注意:如果圖中沒有某節點,但是最後要求的話 輸出也是-1

#include<iostream>#include<stdio.h>#include<cmath>#include<iomanip>#include <map>#include <vector>#include <string>#include <algorithm>#include <sstream>#include <stack>using namespace std;#define MAX 65536typedef struct DES{string des;int distance;}node;int main(){int n;cin>>n;for(int i=0;i<n;i++){map<string,vector<node> > data;map<string,int> flag;int m;cin>>m;for(int j=0;j<m;j++){string source,destination;int distance;cin>>source>>destination>>distance;flag[source]=MAX;flag[destination]=MAX;map<string,vector<node> >::iterator ite;//把兩個節點分別作為鍵儲存在map裡面if((ite=data.find(source))!=data.end()){node tmp={destination,distance};ite->second.push_back(tmp);}else{node tmp={destination,distance};vector<node> tmpVec;tmpVec.push_back(tmp);data.insert(make_pair(source,tmpVec));}if((ite=data.find(destination))!=data.end()){node tmp={source,distance};ite->second.push_back(tmp);}else{node tmp={source,distance};vector<node> tmpVec;tmpVec.push_back(tmp);data.insert(make_pair(destination,tmpVec));}}string source,destination;cin>>source>>destination;stack<string> search;search.push(source);flag[source]=0;//int min=-1;while(!search.empty()){string tmpStackNode=search.top();vector<node> tmpVecNode=data[tmpStackNode];search.pop();for(vector<node>::iterator ite=tmpVecNode.begin();ite!=tmpVecNode.end();ite++){if(flag[tmpStackNode]+ite->distance<flag[ite->des]){search.push(ite->des);flag[ite->des]=flag[tmpStackNode]+ite->distance;}}}map<string,int>::iterator it=flag.find(destination);if(it==flag.end()||it->second==MAX)cout<<-1<<endl;else cout<<it->second<<endl;}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.