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
题目大意:
给出n个点(0~n -1)以及m条街道。随后m行表示着v1 v2 flag lenth time,当flag ==1 时即为单行道。要求输出最短路径以及最快路径。如果最短路径不唯一时,输出耗时最少的那条,如果最快路径不唯一时,输出最少街道的那条。
思路:
由于要求两个最短,那么写两个dijkstra吧,否则你会在形参上面想很久,来回修改,想着如果一个dijkstra用形参控制处理不同的数组与图。
然后根据判断的条件,做出相应的更新,注意,需要额外的记录结点数量。如果用dijkstra +dfs 容易超时。
参考代码:
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
const int inf = 0x7fffffff;
int e[510][510], t[510][510];
vector<int> dis, time, weight, pathl, patht, ansl, anst, node;
vector<bool> vis;
int n, m, st, ed;
void getansl(int v){
ansl.push_back(v);
if(v == st) return;
getansl(pathl[v]);
}
void getanst(int v){
anst.push_back(v);
if(st == v) return;
getanst(patht[v]);
}
int main(){
scanf("%d%d", &n, &m);
dis.resize(n), time.resize(n), weight.resize(n);
pathl.resize(n), patht.resize(n), vis.resize(n), node.resize(n);
fill(dis.begin(), dis.end(), inf);
fill(time.begin(), time.end(), inf);
fill(weight.begin(), weight.end(), inf);
for(int i = 0; i < m; ++i){
int a, b, flag, len ,tt;
scanf("%d%d%d%d%d", &a, &b, &flag, &len, &tt);
e[a][b] = len, t[a][b] = tt;
if(flag != 1) e[b][a] = len, t[b][a] = tt;
}
scanf("%d%d", &st, &ed);
dis[st] = 0;
for(int i = 0; i <n; ++i){
int u = -1, minv = inf;
for(int j = 0; j < n; ++j)
if(!vis[j] && dis[j] < minv) minv = dis[j], u = j;
if(u == -1) break;
vis[u] = true;
for(int v = 0; v < n; ++v)
if(!vis[v] && e[u][v]){
if(dis[u] + e[u][v] < dis[v]){
dis[v] = e[u][v] + dis[u];
pathl[v] = u;
weight[v] = weight[u] + t[u][v];
}else if(dis[u] + e[u][v] == dis[v] && weight[u] + t[u][v] < weight[v]){
pathl[v] = u;
weight[v] = weight[u] + t[u][v];
}
}
}
getansl(ed);
fill(vis.begin(), vis.end(), false);
time[st] = 0;
for(int i = 0; i < n; ++i){
int u = -1, minv = inf;
for(int j = 0; j < n; ++j)
if(!vis[j] && time[j] < minv) minv = time[j], u = j;
if(u == -1) break;
vis[u] = true;
for(int v = 0; v < n; ++v)
if(!vis[v] && t[u][v]){
if(time[u] + t[u][v] < time[v]){
time[v] = time[u] + t[u][v];
patht[v] = u;
node[v] = node[u] + 1;
}else if(time[u] + t[u][v] == time[v] && node[u] + 1 < node[v]){
patht[v] = u;
node[v] = node[u] + 1;
}
}
}
getanst(ed);
if(ansl == anst){
printf("Distance = %d; Time = %d: %d", dis[ed], time[ed], st);
for(int i = anst.size() - 2; i >= 0; --i)
printf(" -> %d", anst[i]);
}else {
printf("Distance = %d: %d", dis[ed], st);
for(int i = ansl.size() - 2; i >= 0; --i)
printf(" -> %d", ansl[i]);
printf("\n");
printf("Time = %d: %d", time[ed], st);
for(int i = anst.size() - 2; i >= 0; --i)
printf(" -> %d", anst[i]);
}
return 0;
}