PAT甲级 1111 Online Map (30 分)dijkstra + dfs

1111 Online Map (30 分)

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+dfs 即可轻松求解。第一次提交最后一个点没AC过27 分。花了几分钟排查直接AC。

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm> 
using namespace std;

const int N = 501,inf = 999999;
int e[N][N],t[N][N],vst[N],dsp[N];
vector<int> pre[N],pre1[N];

vector<int> temppath,respath;//求最短路径
int minpath = inf,mintime = inf;//求最短路径

vector<int> temppath1,respath1;//求最短时间
int mintime1 = inf;//求最短时间 

void dfs(int node){
	temppath.push_back(node);
	if(pre[node].size() == 0){
		int sum_path = 0,sum_time = 0;
		for(int i=temppath.size()-1;i>0;i--){
			int a = temppath[i],b = temppath[i-1];
			sum_path += e[a][b];
			sum_time += t[a][b];
		}
		if(sum_path < minpath){
			minpath = sum_path;
			mintime = sum_time;
			respath = temppath;
		}
		else if(sum_path==minpath && sum_time<mintime){
			mintime = sum_time;
			respath = temppath;
		}
		return ;
	}
	for(int i=0;i<pre[node].size();i++){
		dfs(pre[node][i]);
		temppath.pop_back();
	}
}

void dfs1(int node){
	temppath1.push_back(node);
	if(pre1[node].size() == 0){
		int sum_time = 0;
		for(int i=temppath1.size()-1;i>0;i--){
			int a = temppath1[i],b = temppath1[i-1];
			sum_time += t[a][b];
		}
		if(sum_time < mintime1){
			mintime1 = sum_time;
			respath1 = temppath1;
		}
		else if(sum_time == mintime1 && temppath1.size() < respath1.size()){
			respath1 = temppath1;
		}
		
		return ;
	}
	for(int i=0;i<pre1[node].size();i++){
		dfs1(pre1[node][i]);
		temppath1.pop_back();
	}
}


int main(){
	fill(e[0],e[0]+N*N,inf);
	fill(t[0],t[0]+N*N,inf);
	fill(vst,vst+N,false);
	fill(dsp,dsp+N,inf);
	
	int n,m,start,dest;
	cin>>n>>m;
	int v1,v2,way,length,time;
	for(int i=0;i<m;i++){
		cin>>v1>>v2>>way>>length>>time;
		if(way == 1){
			e[v1][v2] = length;
			t[v1][v2] = time;
		}else{
			e[v1][v2] = e[v2][v1] = length;
			t[v1][v2] = t[v2][v1] = time;
		}
	}
	cin>>start>>dest;
	dsp[start] = 0;
	for(int i = 0;i<n;i++){
		int u = -1,min = inf;
		for(int j = 0;j<n;j++){
			if(vst[j] == false && dsp[j] < min){
				u = j;
				min = dsp[j];
			}
		}
		if(u == -1)
			break;
		vst[u] = true;
		for(int v = 0;v<n;v++){
			if(vst[v]==false && dsp[u]+e[u][v] < dsp[v]){
				dsp[v] = dsp[u]+e[u][v];
				pre[v].clear();
				pre[v].push_back(u);
			}
			else if(vst[v]==false && dsp[u]+e[u][v] == dsp[v]){
				pre[v].push_back(u);
			}	
		}
	}
	dfs(dest);
	
	//最短时间 
	fill(vst,vst+N,false);//记得重置 
	fill(dsp,dsp+N,inf);
	dsp[start] = 0;
	for(int i = 0;i<n;i++){
		int u = -1,min = inf;
		for(int j = 0;j<n;j++){
			if(vst[j] == false && dsp[j] < min){
				u = j;
				min = dsp[j];
			}
		}
		if(u == -1)
			break;
		vst[u] = true;
		for(int v = 0;v<n;v++){
			if(vst[v]==false && dsp[u]+t[u][v] < dsp[v]){
				dsp[v] = dsp[u]+t[u][v];
				pre1[v].clear();
				pre1[v].push_back(u);
			}
			else if(vst[v]==false && dsp[u]+t[u][v] == dsp[v]){
				pre1[v].push_back(u);
			}	
		}
	}
	dfs1(dest);
	
	if(respath == respath1){
		printf("Distance = %d; Time = %d: ",minpath,mintime1);
		for(int i=respath.size()-1;i>=0;i--)
			printf("%d%s",respath[i],i!=0?" -> ":"");
	}else{
		printf("Distance = %d: ",minpath);
		for(int i=respath.size()-1;i>=0;i--)
			printf("%d%s",respath[i],i!=0?" -> ":"");
		cout<<endl;
		printf("Time = %d: ",mintime1);
		for(int i=respath1.size()-1;i>=0;i--)
			printf("%d%s",respath1[i],i!=0?" -> ":"");
	}
	

	return 0;
}

 

    原文作者:Dijkstra算法
    原文地址: https://blog.csdn.net/qq_29762941/article/details/85635112
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞