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
这道题里面要求两种最短路径,要用两次迪杰斯特拉算法。代码量比较大,按理说没啥难点,但最后一个测试点调了好久都过不去,先放一放吧,希望可以得到高人指点
代码
#include<bits/stdc++.h>
using namespace std;
const int inf = 999999;
int source, endCity, road[510][510];
int costtime[510][510], minDis[510], minTime[510], pathD[510], pathT[510],timeFast[510];
bool visit[510],nodeNum[510];
int cityNum, roadNum,flag=1;
int main(){
fill(road[0], road[0] + 510 * 510, inf);
fill(costtime[0], costtime[0] + 510 * 510, inf);
fill(minDis, minDis + 510, inf);
fill(timeFast, timeFast + 510, inf);
fill(minTime, minTime + 510, inf);
fill(pathD, pathD + 510, -1);//存储长度最短的路径结点信息
fill(pathT, pathT + 510, -1);//存储时间最短的路径结点信息
cin >> cityNum >> roadNum;
int a, b, c, d, e;
for (int i = 0; i < roadNum;i++){
cin >> a >> b >> c >> d >> e;
if(c==0){
road[a][b] = road[b][a] = d;
costtime[a][b] = costtime[b][a] = e;
}//第三个数字是0代表是双向路
else if(c==1){
road[a][b] = d;
costtime[a][b] = e;
}
}
cin >> source >> endCity;
minDis[source] = 0;
minTime[source] = 0;
timeFast[source] = 0;
for (int i = 0; i < cityNum;i++){
int u = -1, minn = inf;
for (int j = 0; j < cityNum;j++){
if(visit[j]==false&&minDis[j]<minn){
u = j;
minn = minDis[j];
}
}
if(u==-1)
break;
visit[u] = true;
for (int v = 0; v < cityNum;v++){
if(visit[v]==false&&road[u][v]!=inf){
if(minDis[u]+road[u][v]<minDis[v]){
minDis[v] = minDis[u] + road[u][v];
timeFast[v] = timeFast[u] + costtime[u][v];
pathD[v] = u;
}
else if(minDis[u]+road[u][v]==minDis[v]){
if(timeFast[v]>timeFast[u]+costtime[u][v]){
timeFast[v] = timeFast[u] + costtime[u][v];
pathD[v] = u;
}//如果最短路径相同,选择花费时间最少的
}
}
}
}
fill(visit, visit + 510, false);
for (int i = 0; i < cityNum;i++){
int u = -1, minn = inf;
for (int j = 0; j < cityNum;j++){
if(visit[j]==false&&minTime[j]<minn){
u = j;
minn = minTime[j];
}
}
if(u==-1)
break;
visit[u] = true;
for (int v = 0; v < cityNum;v++){
if(visit[v]==false&&costtime[u][v]!=inf){
if(minTime[u]+costtime[u][v]<minTime[v]){
minTime[v] = minTime[u] + costtime[u][v];
pathT[v] = u;
}
else if(minTime[u]+costtime[u][v]==minTime[v]&&nodeNum[u]+1<nodeNum[v]){
pathT[v] = u;
nodeNum[v] = nodeNum[u] + 1;
}
/*选择经过结点数最少的最快路径的时候可以不需要另外加判断,因为结点是由多到少选择的,它首先选择的就是经过结点数最少的路径*/
}
}
}
for (int i = 0; i < cityNum; i++){
if(pathD[i]!=pathT[i])
flag = 0;
}//flag=0代表长短最短的和时间最短的路径不相同
int stack[cityNum],stack2[cityNum],top = -1,top2=-1;
a = endCity;
vector<int> m1, m2;
while(a!=-1){
m1.push_back(a);
stack[++top] = a;
a = pathD[a];
}//实现逆序输出
a = endCity;
while(a!=-1){
m2.push_back(a);
stack2[++top2] = a;
a = pathT[a];
}
if(m1!=m2)
flag = 0;
if(flag==0){
printf("Distance = %d: %d", minDis[endCity], stack[top--]);
while(top!=-1){
printf(" -> %d", stack[top--]);
}
printf("\n");
}
else{
printf("Distance = %d; ", minDis[endCity]);
}
printf("Time = %d: %d", minTime[endCity], stack2[top2--]);
while(top2!=-1){
printf(" -> %d", stack2[top2--]);
}
return 0;
}