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算法的初始化条件。g[][]每条边都要初始化成INF,d[]也是INF,used[]全是false,路径还原前驱数组pred[]全是-1
#include <bits/stdc++.h>
#define N 505
#define INF 100000005
using namespace std;
typedef pair<int,int> pii;
pii g[N][N]; //错因1,切记,易忘:初始化须将每条边设为INF
int n,m,d[N],dispred[N],t[N],nodenum[N],timepred[N];
bool used[N];
void dijkstraDis(int be);
void dijkstraTime(int be);
vector<int> getPath(int a[],int ed);
int main() {
pii tp={INF,INF};
fill(g[0],g[0]+N*N,tp); //对图g的初始化
int v1,v2,l,ti,sp,be,en; //中间变量
cin>>n>>m;
for (int i=0;i<m;i++) {
cin>>v1>>v2>>sp>>l>>ti;
g[v1][v2]={l,ti};
if (sp==0) g[v2][v1]={l,ti}; //初始化双向边
}
cin>>be>>en; //起点 终点
dijkstraDis(be); //对距离进行最短路
dijkstraTime(be); //对时间进行最短路
vector<int> dip=getPath(dispred,en); //路径还原的最短距离
vector<int> tip=getPath(timepred,en); //路径还原的最短时间
printf("Distance = %d",d[en]);
if (dip==tip) printf("; Time = %d: ",t[en]);
else {
printf(": ");
for (int i=(int)dip.size()-1;i>=0;i--) {
printf("%d",dip[i]);
if (i!=0) printf(" -> ");
}
printf("\nTime = %d: ",t[en]);
}
for (int i=(int)tip.size()-1;i>=0;i--) {
printf("%d",tip[i]);
if (i!=0) printf(" -> ");
}
printf("\n");
return 0;
}
void dijkstraDis(int be) { //条件最短路,就需要开一个辅助数组记录每个节点条件的值,一般一维数组足矣
fill(d,d+N,INF);
fill(used,used+N,false);
fill(dispred,dispred+N,-2);
d[be]=0;
dispred[be]=-1;
while (true) {
int v=-1;
for (int u=0;u<n;u++)
if (!used[u]&&(v==-1||d[u]<d[v])) v=u;
if (v==-1) break;
used[v]=true;
for (int u=0;u<n;u++) {
if (d[u]>d[v]+g[v][u].first) {
d[u]=d[v]+g[v][u].first;
dispred[u]=v;
t[u]=t[v]+g[v][u].second; //不要忘了这句
}
else if (d[u]==d[v]+g[v][u].first&&t[u]>t[v]+g[v][u].second) {
t[u]=t[v]+g[v][u].second;
dispred[u]=v;
}
}
}
}
void dijkstraTime(int be) {
fill(t,t+N,INF);
fill(used,used+N,false);
fill(timepred,timepred+N,-2);
fill(nodenum,nodenum+N,0);
t[be]=0;
timepred[be]=-1;
while (true) {
int v=-1;
for (int u=0;u<n;u++)
if (!used[u]&&(v==-1||t[u]<t[v])) v=u;
if (v==-1) break;
used[v]=true;
for (int u=0;u<n;u++) {
if (t[u]>t[v]+g[v][u].second) {
t[u]=t[v]+g[v][u].second;
timepred[u]=v;
nodenum[u]=nodenum[v]+1;
}
else if (t[u]==t[v]+g[v][u].second&&nodenum[u]>nodenum[v]+1) {
timepred[u]=v;
nodenum[u]=nodenum[v]+1;
}
}
}
}
vector<int> getPath(int a[],int ed) {
vector<int> vi;
for (;ed!=-1;ed=a[ed]) vi.push_back(ed);
return vi; //路径的反向,我没有逆置
}