sicily 1031. Campus (优先队列优化的dijistra模板)

http://soj.sysu.edu.cn/1031

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

de style="color: rgb(51, 51, 51); font-size: 15px; line-height: 22.5px;" >
1
2
South.xiaolitang South.xiongdelong 2
South.xiongdelong Zhuhai.liyuan 100
South.xiongdelong South.xiaolitang
de>

Sample Output

de style="color: rgb(51, 51, 51); font-size: 15px; line-height: 22.5px;" >
2
de>

Problem Source

ZSUACM Team Member

  1. /Dijkstra邻接表做法  
  2. //邻接表使得计算复杂度成为O(E*n),但图为稀疏图时效率比邻接矩阵高,但当图为稠密图时,E与n^2同阶,计算复杂度最坏可到O(n^3)  
  3. //Dijkstra优先队列实现(相当于堆优化)  
  4. //优先队列的实现的Dijstra可以使得算法在处理稠密图时速度也比使用邻接矩阵要快  
  5. //原因在于插入队列的元素的前提是边关系必须满足可松弛条件,对于稠密图而言,可松弛条件是常常不满足的  
  6. //因此插入队列的元素便比边数少很多  
  7. //下面使用vector容器实现邻接表的,因为直观易懂  
  8. //G[u]存放的是与结点u邻接的所有结点  
  9. //遍历邻接边:for(int i = 0;i < G[u].size();++i)  

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 1<<30
#define maxn 10000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;

bool cmp(int a,int b){
return a>b;
}
struct edge{
int u,v,w;
edge(int uu,int vv,int ww){
u=uu;
v=vv;
w=ww;
}
};
typedef pair<int,int>pii;
int lowcost[maxn];
int cnt;
int dij(int st,int ed,vector<edge>G[]){
priority_queue<pii>q;
for(int i=1;i<=cnt;i++)
lowcost[i]=(i==st?0:INF);
q.push(make_pair(lowcost[st],st));//make_pair
while(!q.empty()){
pii u=q.top();
q.pop();
int x=u.second;
if(u.first!=lowcost[x])continue;
for(int i=0;i<G[x].size();i++){//vector构建的邻接表,存边的信息
int y=G[x][i].v;
int w=G[x][i].w;
if(lowcost[y]>lowcost[x]+w){
lowcost[y]=lowcost[x]+w;
q.push(make_pair(lowcost[y],y));
}
}
}
if(lowcost[ed]==INF)return -1;
else return lowcost[ed];
}
map<string,int>mp;
int main()
{
#ifndef ONLINE_JUDGE
//freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int T,n,w;
cin>>T;
while(T--){
cin>>n;
mp.clear();
string s,t;
cnt=0;
vector<edge>G[maxn];
for(int i=1;i<=n;i++){
cin>>s>>t>>w;
if(!mp.count(s))mp.insert(make_pair(s,++cnt));
if(!mp.count(t))mp.insert(make_pair(t,++cnt));
edge E1(mp[s],mp[t],w);
edge E2(mp[t],mp[s],w);
G[mp[s]].push_back(E1);
G[mp[t]].push_back(E2);
}
cin>>s>>t;
if(s==t)cout<<0<<endl;
else if(!mp.count(s)||!mp.count(t))cout<<-1<<endl;
else cout<<dij(mp[s],mp[t],G)<<endl;
}
return 0;
}




你可能感兴趣的:(sicily 1031. Campus (优先队列优化的dijistra模板))