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 65536
typedef 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;
}
}
本文介绍了一个基于中山大学四个校区布局的最短路径问题,通过Dijkstra算法实现求解任意两点间最短距离,并提供了完整的代码实现。
7569

被折叠的 条评论
为什么被折叠?



