时间限制
300 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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
这道题的代码量挺大,但是思路是很简单的,相关的题在前面已经出现过。
one-way是指单行道。
需要两次最短路计算和两次DFS计算,其中最短路计算用一个函数就可以了,思路完全一样,只是记录结果的参数、需要使用的路径花费不一样。
所有的数据中间值、结果都是int范围内的,不需要考虑溢出或使用long long。
注意输出路径时的方向。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 501;
const int inf = 0x3fffffff;
//分别表示起点、终点、道路顶点数、道路条数、路径最短数组、时间最短数组,顶点之间的路径花费、时间花费。
int s,T,n,m,d[maxn],t[maxn],g[maxn][maxn],gm[maxn][maxn];
bool vis[maxn] = {0};
vector<int> pre[maxn],tempPath,path1,path2;
int maxTime = inf,minD = inf; //最短用时,最短距离初始为无穷大
void Dijkstra(int st,int a[],int g[maxn][maxn]){
fill(a,a+maxn,inf);
a[st] = 0;
for(int i = 0;i < n;i++){
int u = -1,Min = inf;
for(int j = 0;j < n;j++){
if(vis[j]==0&&a[j]<Min){
u = j;
Min = a[j];
}
}
if(u==-1) return;
vis[u] = 1;
for(int v = 0;v < n;v++){
if(vis[v]==0&&g[u][v]!=inf){
if(a[v]>a[u]+g[u][v]){
pre[v].clear();
pre[v].push_back(u);
a[v] = a[u]+g[u][v];
}
else if(a[v]==a[u]+g[u][v]){
pre[v].push_back(u);
}
}
}
}
}
void DFS1(int v){
if(v==s){ //递归边界,到达起点
int sum = 0;
tempPath.push_back(s);
for(int i = tempPath.size()-1;i;i--){
sum += gm[tempPath[i]][tempPath[i-1]];
}
if(sum>0&&sum<maxTime){
path1 = tempPath;
maxTime = sum;
}
tempPath.pop_back();
return;
}
tempPath.push_back(v);
for(int i = 0;i < pre[v].size();i++){
DFS1(pre[v][i]);
}
tempPath.pop_back();
}
void DFS2(int v){
if(v==s){ 递归边界,到达起点
tempPath.push_back(s);
if(tempPath.size()<minD){
path2 = tempPath;
minD = tempPath.size();
}
tempPath.pop_back();
return;
}
tempPath.push_back(v);
for(int i = 0;i < pre[v].size();i++){
DFS2(pre[v][i]);
}
tempPath.pop_back();
}
void print(vector<int> &vi){
for(int i = vi.size()-1;i >= 0;i--){
if(i) cout << vi[i] << " -> ";
else cout << vi[i] << endl;
}
}
int main(){
int v1,v2,flag,len,time;
cin >> n >> m;
fill(g[0],g[0]+maxn*maxn,inf);
fill(gm[0],gm[0]+maxn*maxn,inf);
for(int i = 0;i < m;i++){
scanf("%d %d %d %d %d",&v1,&v2,&flag,&len,&time);
g[v1][v2] = len;
gm[v1][v2] = time;
if(flag!=1){
g[v2][v1] = g[v1][v2];
gm[v2][v1] = gm[v1][v2];
}
}
cin >> s >> T;
Dijkstra(s,d,g);
DFS1(T);
fill(vis,vis+maxn,0);
Dijkstra(s,t,gm);
DFS2(T);
if(path1==path2){
printf("Distance = %d; Time = %d: ",d[T],t[T]);
print(path1);
} else {
printf("Distance = %d: ",d[T]);
print(path1);
printf("Time = %d: ",t[T]);
print(path2);
}
return 0;
}