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), 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
这题考察最短路径,使用Dijstra即可求解,我们第一次求最短路,是求路径最短,如果路径一样,则求时间花费最少。
第二次求最短路,是求时间最少,如果时间一样,则求换乘最少。
#include <iostream> #include <vector> #define INF 99999999 using namespace std; int N, M, a, b, oneway, len, tim; int L[550][550] = {0}, T[550][550] = {0}, vis[550] = {0}; int start, end0; int main() { // input scanf("%d%d", &N, &M); while(M--) { scanf("%d%d%d%d%d", &a, &b, &oneway, &len, &tim); L[a][b] = len; T[a][b] = tim; if(oneway != 1) { L[b][a] = len; T[b][a] = tim; } } scanf("%d%d", &start, &end0); // Len & Time int dis[550], prevDis[550], weight[550]; fill(weight, weight + 550, INF); fill(dis, dis + 550, INF); for(int i = 0; i < 550; i++) prevDis[i] = i; dis[start] = 0; weight[start] = 0; while(true) { int u = -1, minn = INF; for(int v = 0; v < N; v++) { if(!vis[v] && dis[v] < minn) { minn = dis[v]; u = v; } } if(u == -1) break; vis[u] = 1; for(int v = 0; v < N; v++) { if(!vis[v] && L[u][v] != 0) { if(L[u][v] + dis[u] < dis[v]) { dis[v] = L[u][v] + dis[u]; prevDis[v] = u; weight[v] = weight[u] + T[u][v]; } else if(L[u][v] + dis[u] == dis[v] && T[u][v] + weight[u] < weight[v]) { prevDis[v] = u; weight[v] = weight[u] + T[u][v]; } } } } // Time & Node int time[550], prevTime[550], node[550]; fill(time, time + 550, INF); fill(node, node + 550, INF); fill(vis, vis + 550, 0); for(int i = 0; i < 550; i++) prevTime[i] = i; time[start] = 0; node[start] = 0; while(true) { int u = -1, minn = INF; for(int v = 0; v < N; v++) { if(!vis[v] && time[v] < minn) { minn = time[v]; u = v; } } if(u == -1) break; vis[u] = 1; for(int v = 0; v < N; v++) { if(!vis[v] && T[u][v] != 0) { if(T[u][v] + time[u] < time[v]) { time[v] = T[u][v] + time[u]; prevTime[v] = u; node[v] = node[u] + 1; } else if(T[u][v] + time[u] == time[v] && node[u] + 1 < node[v]) { prevTime[v] = u; node[v] = node[u] + 1; } } } } // deal with path int cp = end0, ans_dis = 0, ans_tim = 0, cp1 = end0, ans_dis1 = 0, ans_tim1 = 0; vector<int> ans1, ans2; while(cp != start) { int tmp = cp; ans1.push_back(cp); cp = prevDis[cp]; ans_dis += L[cp][tmp]; ans_tim += T[cp][tmp]; } while(cp1 != start) { int tmp = cp1; ans2.push_back(cp1); cp1 = prevTime[cp1]; ans_dis1 += L[cp1][tmp]; ans_tim1 += T[cp1][tmp]; } if(ans1 != ans2) { printf("Distance = %d: %d", ans_dis, start); for(int i = ans1.size() - 1; i >= 0; i--) printf(" -> %d", ans1[i]); putchar('\n'); printf("Time = %d: %d", ans_tim1, start); for(int i = ans2.size() - 1; i >= 0; i--) printf(" -> %d", ans2[i]); putchar('\n'); } else { printf("Distance = %d; Time = %d: %d", ans_dis, ans_tim, start); for(int i = ans1.size() - 1; i >= 0; i--) printf(" -> %d", ans1[i]); putchar('\n'); } return 0; }