最短路径-Dijkstra

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<map>
#include<vector>
#include<queue>

using namespace std;
const int INF=1000000;
const int MAX=200;
int dis[MAX];
int n;//结点数 
int _min;
typedef pair<int,int>pii;
struct edge{
	int u;
	int v;
	int w;
	edge(int uu,int vv,int ww){
		u=uu;
		v=vv;
		w=ww;
	}
};
int dijkstra(int start,int end,vector<edge> *G){
	//优先队列默认小元素先先出队 
	priority_queue<pii> q;
	for(int i=0;i<n;i++){
		dis[i]=(i==start?0:INF);
	}
	//将起点插入队列,pair默认是优先处理first元素,
	//故插入优先队列先弹出队列的优先级是依据dis[]大小   
	q.push(make_pair(dis[start],start));
	while(!q.empty()){
		pii u=q.top();
		q.pop();
		int x=u.second;
		/*可避免结点的重复拓展,提高优先队列处理速度。
		因为Dijkstra算法对每个结点都会进行一次标号。  */
		if(u.first != dis[x])
			continue;
		for(int i=0;i<G[x].size();i++){  
            int y = G[x][i].v;  
            int w = G[x][i].w;  
            if(dis[y] > dis[x] + w) {  
                dis[y] = dis[x] + w;  
                q.push(make_pair(dis[y],y));  
            }  
        }  
	}
	if(dis[end]==INF)
		return -1;
	else
		return dis[end];
}

int main(){
	int T,road,dis;
	string start,end;
	cin>>T;
	while(T--){
		cin>>road;
		map<string,int> campus;
		vector<edge> G[MAX];
		n=0;
		for(int i=0;i<road;i++){
			cin>>start>>end>>dis;
			if(!campus.count(start)){
				campus[start]=n++;
			}
			if(!campus.count(end)){
				campus[end]=n++;
			}
			edge E1(campus[start],campus[end],dis);
			edge E2(campus[end],campus[start],dis);	
			G[campus[start]].push_back(E1);
			G[campus[end]].push_back(E2);
	    }
	    cin>>start>>end;
		if(start==end){
			cout<<0<<endl;
		}
		else if(!campus.count(start)||!campus.count(end)){
			cout<<-1<<endl;
		}
		else{
			cout<<dijkstra(campus[start],campus[end],G)<<endl;	
		}	
	}
	return 0;
} 
#include<iostream>
#include<map>

using namespace std;
const int INF=10000001;
const int MAX=200;
int arc[MAX][MAX];
int visited[MAX];
int startDis[MAX];
int n;
int _min;
int dijkstra(int start,int end){
    for(int i=0;i<MAX;i++)
        visited[i]=0;
    for(int i=0;i<n;i++){
        if(i==start)
            startDis[i]=0;
        else
            startDis[i]=INF;
    }
    int tt;
    for(int i=0;i<n;i++){
        _min=INF;
        tt=start;
        for(int j=0;j<n;j++){
            if(!visited[j]&&startDis[j]<_min){
                _min=startDis[j];
                tt=j;
            }
        }
        visited[tt]=1;
        for(int j=0;j<n;j++){
            startDis[j]=min(arc[tt][j]+startDis[tt],startDis[j]);
        }
    }
    if(visited[end])
        return startDis[end];
    else
        return -1; 
    
}

int main(){
    int T,road,dis;
    string start,end;
    cin>>T;
    while(T--){
        cin>>road;
        for(int i=0;i<MAX;i++){
            for(int j=0;j<MAX;j++){
                if(i==j)
                    arc[i][j]=0;
                else
                    arc[i][j]=INF;
            }   
        }
        map<string,int> campus;
        n=0;
        for(int i=0;i<road;i++){
            cin>>start>>end>>dis;
            if(!campus.count(start)){
                campus[start]=n++;
            }
            if(!campus.count(end)){
                campus[end]=n++;
            }
            arc[campus[start]][campus[end]]=dis;
            arc[campus[end]][campus[start]]=dis;
        }
        cin>>start>>end;
        if(start==end){
            cout<<0<<endl;
        }
        	else if(!campus.count(start)||!campus.count(end)){
			cout<<-1<<endl;
		}
        else{
            cout<<dijkstra(campus[start],campus[end])<<endl;    
        }   
    }
    return 0;
}                                 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值