【题解】【PAT甲】1111 Online Map (30 分)(Dijkstra)

Dijkstra算法:最短路径与最快时间并举
本文介绍如何使用Dijkstra算法解决在线地图推荐中,找到起点到终点的最短距离路径和最短时间路径。通过两次遍历和优化策略,确保在最短路径存在的情况下,返回时间最优且路径最少的解决方案。

题目链接

 PTA | 程序设计类实验辅助教学平台

题目描述

Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is guaranteed that a path exists for any request.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (2≤N≤500), and M, being the total number of streets intersections on a map, and the number of streets, respectively. Then M lines follow, each describes a street in the format:

V1 V2 one-way length time

where V1 and V2 are the indices (from 0 to N−1) of the two ends of the street; one-way is 1 if the street is one-way from V1 to V2, or 0 if not; length is the length of the street; and time is the time taken to pass the street.

Finally a pair of source and destination is given.

Output Specification:

For each case, first print the shortest path from the source to the destination with distance D in the format:

Distance = D: source -> v1 -> ... -> destination

Then in the next line print the fastest path with total time T:

Time = T: source -> w1 -> ... -> destination

In case the shortest path is not unique, output the fastest one among the shortest paths, which is guaranteed to be unique. In case the fastest path is not unique, output the one that passes through the fewest intersections, which is guaranteed to be unique.

In case the shortest and the fastest paths are identical, print them in one line in the format:

Distance = D; Time = T: source -> u1 -> ... -> destination

Sample Input 1:

10 15
0 1 0 1 1
8 0 0 1 1
4 8 1 1 1
3 4 0 3 2
3 9 1 4 1
0 6 0 1 1
7 5 1 2 1
8 5 1 2 1
2 3 0 2 2
2 1 1 1 1
1 3 0 3 1
1 4 0 1 1
9 7 1 3 1
5 1 0 5 2
6 5 1 1 2
3 5

结尾无空行

Sample Output 1:

Distance = 6: 3 -> 4 -> 8 -> 5
Time = 3: 3 -> 1 -> 5

结尾无空行

Sample Input 2:

7 9
0 4 1 1 1
1 6 1 1 3
2 6 1 1 1
2 5 1 2 2
3 0 0 1 1
3 1 1 1 3
3 2 1 1 2
4 5 0 2 2
6 5 1 1 2
3 5

结尾无空行

Sample Output 2:

Distance = 3; Time = 4: 3 -> 2 -> 5

结尾无空行

题目大意

 给你一个图,有可能是单向有可能是双向的道路,给出每条边的时间和距离,求给出起点到终点的最短路径和花费的最少时间,如果最短路径有多条求时间最短的路径,如果时间最短的路径有多条求经过节点最少的

解题思路

 需要用两遍Dijkstra,一遍求最短距离,一遍求最短时间,因为要获得明确地路径,所以可以设一个指向前面节点的数组dispre,然后从最后节点向起点遍历,将便利的到的数组reverse一下就可以得到答案

if(dis[v]>dis[u]+dg[u][v]){
	dis[v]=dis[u]+dg[u][v];
	dispre[v]=u;        //指向前面节点
}
void dfs1(int x){    //从最后一个节点向前遍历
	dv.push_back(x);
	if(x==b)	return;
	dfs1(dispre[x]);
}

求最短长度的路径

题目要求求最短长度的路径,如果有重复的就取时间最短的

所以用一个数组weight来计算从u到v的最短时间

当两个节点之间有更短的路径更新的时候直接替换,如果两个节点的最短路径有重复的,就选择时间花费短的那一条路径,就可以得到相应结果

for(int v=0;v<n;v++){
	if(dis[v]>dis[u]+dg[u][v]){        //如果有最短的
		dis[v]=dis[u]+dg[u][v];
		dispre[v]=u;                    //直接替换
		weight[v]=weight[u]+tg[u][v];
	}
	else if(dis[v]==dis[u]+dg[u][v]&&weight[v]>weight[u]+tg[u][v]){
        //如果最短的重复,判断时间最短的那条替换
		weight[v]=weight[u]+tg[u][v];
		dispre[v]=u;
	}
}

求最短时间的路径

最短时间路径要求是求最短时间,如果多条重复的时间最短路径,取节点最少的那个

这里需要一个额外的数组记录到u节点最少的节点个数,如果uv之间的路径被更新为最短,直接让

nums[v]=nums[u]+1;

如果出现时间相等而且都是时间最短的路径的时候,取nums[u]最小的那个

if(dis[v]>dis[u]+tg[u][v]){
	dis[v]=dis[u]+tg[u][v];
    dispre[v]=u;
	nums[v]=nums[u]+1;
}
else if(dis[v]==dis[u]+tg[u][v]&&nums[v]>nums[u]+1){
	nums[v]=nums[u]+1;
	dispre[v]=u;
}

题解

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
int dg[505][505];
int tg[505][505];
vector<int> dv;
vector<int> tv;
map<vector<int>,int> ma;
int dis[505];
int visited[505];
int dispre[505];
int weight[505];
int nums[505];
int b,e;
int mindis;
void dfs1(int x){
	dv.push_back(x);
	if(x==b)	return;
	dfs1(dispre[x]);
}
void dfs2(int x){
	tv.push_back(x);
	if(x==b)	return;
	dfs2(dispre[x]);
}
int main(){
	int n,m;
	cin>>n>>m;
	int v1,v2,one,len,time;
	memset(dg,INF,sizeof(dg));
	memset(tg,INF,sizeof(tg));
	memset(dis,INF,sizeof(dis));
	for(int i=0;i<m;i++){
		cin>>v1>>v2>>one>>len>>time;
		dg[v1][v2]=len;
		tg[v1][v2]=time;
		if(one==0){
			dg[v2][v1]=len;
			tg[v2][v1]=time;
		}
	}
	cin>>b>>e;
	dis[b]=0;
	for(int i=0;i<n;i++){
		int mmin=INF;
		int u=-1;
		for(int j=0;j<n;j++){
			if(visited[j]==0&&dis[j]<mmin){
				u=j;
				mmin=dis[j];
			}
		}
		if(u==-1)	break;
		visited[u]=1;
		for(int v=0;v<n;v++){
			if(dis[v]>dis[u]+dg[u][v]){
				dis[v]=dis[u]+dg[u][v];
				dispre[v]=u;
				weight[v]=weight[u]+tg[u][v];
			}
			else if(dis[v]==dis[u]+dg[u][v]&&weight[v]>weight[u]+tg[u][v]){
				weight[v]=weight[u]+tg[u][v];
				dispre[v]=u;
			}
		}
	} 
	dfs1(e);
	reverse(dv.begin(),dv.end());
	mindis=dis[e];
	memset(dis,INF,sizeof(dis));
	memset(visited,0,sizeof(visited));
	memset(dispre,0,sizeof(dispre));
	dis[b]=0;
	for(int i=0;i<n;i++){
		int mmin=INF;
		int u=-1;
		for(int j=0;j<n;j++){
			if(visited[j]==0&&dis[j]<mmin){
				u=j;
				mmin=dis[j];
			}
		}
		if(u==-1)	break;
		visited[u]=1;
		for(int v=0;v<n;v++){
			if(dis[v]>dis[u]+tg[u][v]){
				dis[v]=dis[u]+tg[u][v];
				dispre[v]=u;
				nums[v]=nums[u]+1;
			}
			else if(dis[v]==dis[u]+tg[u][v]&&nums[v]>nums[u]+1){
				nums[v]=nums[u]+1;
				dispre[v]=u;
			}
		}
	}
	dfs2(e);
	reverse(tv.begin(),tv.end());
	ma[tv]++;
	ma[dv]++;
	if(ma[tv]==2){
		printf("Distance = %d; Time = %d: ",mindis,dis[e]);
		for(int i=0;i<tv.size();i++){
			if(i!=0)	cout<<" -> ";
			cout<<tv[i];
		}
	}
	else{
		printf("Distance = %d: ",mindis);
		for(int i=0;i<dv.size();i++){
			if(i!=0)	cout<<" -> ";
			cout<<dv[i];
		}
		cout<<endl;
		printf("Time = %d: ",dis[e]);
		for(int i=0;i<tv.size();i++){
			if(i!=0)	cout<<" -> ";
			cout<<tv[i];
		}
	}
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值