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,参考dalao的代码如下:
满分代码如下:
#include<bits/stdc++.h>
using namespace std;
int N,M,source,des,shortest[2];
//source表示起点,des表示终点
//shortest存储最短路径的长度(下标为0)和最快路径的时间(下标为1)
int graph[505][505][2];
//图的邻接矩阵,下标为0的邻接矩阵表示长度,下标为1的邻接矩阵表示时间
int dis[505],past[505],num[505];
//存储到各点的最短距离,各点的父节点
//还有一个辅助数组num,求最短路径时存储时间,求最快路径时存储结点的个数
bool visit[505];//是否已经访问过
vector<int>path[2];
//下标为0时存储最短路径,下标为1时存储最快路径
void dijkstra(int index){
//index=0时表示求最短路径,index=1表示求的是最快路径
while(!visit[des]){
//还没有访问到终点
int v=-1,MIN=INT_MAX;
for(int i=0;i<N;i++){
//求出目前距离访问最短的未访问的结点
if(!visit[i]&&MIN>dis[i]){
MIN=dis[i];
v=i;
}
}
if(v==-1) return;
visit[v]=true;
for(int i=0;i<N;i++){
//遍历v能访问到的结点
if(!visit[i]&&graph[v][i][index]!=0&&dis[i]>dis[v]+graph[v][i][index]){
dis[i]=dis[v]+graph[v][i][index];
past[i]=v;//更新父节点
if(index==0){
//求最短路径
num[i]=num[v]+graph[v][i][1];//更新时间
}else if(index==1){
num[i]=num[v]+1;//更新结点上的个数
}
}else if(graph[v][i][index]!=0&&dis[i]==dis[v]+graph[v][i][index]){
//距离相等
if(index==0&&num[i]>num[v]+graph[v][i][1]){
//时间更短
past[i]=v;
num[i]=num[v]+graph[v][i][1];
}else if(graph[v][i][index]!=0&&index==1&&num[i]>num[v]+1){
past[i]=v;
num[i]=num[v]+1;
}
}
}
}
shortest[index]=dis[des];
}
void dfs(int v,int index){
//index=0求最短路径,=1求最快路径
if(v==source){
path[index].push_back(v);
return;
}
dfs(past[v],index);
path[index].push_back(v);
}
bool cmp(){
//比较两个路径是否完全一致
if(path[0].size()!=path[1].size())
return false;
for(int i=0;i<path[0].size();i++){
if(path[0][i]!=path[1][i])
return false;
}
return true;
}
void output(int index){
//输出路径
for(int i=0;i<path[index].size();i++){
printf("%s%d",i>0?" -> ":"",path[index][i]);
}
printf("\n");
}
int main(){
scanf("%d%d",&N,&M);
while(M--){
int a,b,c,d,e;
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
graph[a][b][0]=d;
graph[a][b][1]=e;
if(c==0){
graph[b][a][0]=d;
graph[b][a][1]=e;
}
}
scanf("%d%d",&source,&des);
for(int i=0;i<2;i++){
//两次用dijkstra+DFS算法
fill(visit,visit+N,false);
fill(dis,dis+N,INT_MAX);
fill(num,num+N,INT_MAX);
dis[source]=0;
num[source]=0;
dijkstra(i);
dfs(des,i);
}
if(cmp()){
printf("Distance = %d; Time = %d: ",shortest[0],shortest[1]);
output(0);
}else{
printf("Distance = %d: ",shortest[0]);
output(0);
printf("Time = %d: ",shortest[1]);
output(1);
}
return 0;
}