• 1111 Online Map


    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:

    1. 10 15
    2. 0 1 0 1 1
    3. 8 0 0 1 1
    4. 4 8 1 1 1
    5. 3 4 0 3 2
    6. 3 9 1 4 1
    7. 0 6 0 1 1
    8. 7 5 1 2 1
    9. 8 5 1 2 1
    10. 2 3 0 2 2
    11. 2 1 1 1 1
    12. 1 3 0 3 1
    13. 1 4 0 1 1
    14. 9 7 1 3 1
    15. 5 1 0 5 2
    16. 6 5 1 1 2
    17. 3 5

    Sample Output 1:

    1. Distance = 6: 3 -> 4 -> 8 -> 5
    2. Time = 3: 3 -> 1 -> 5

    Sample Input 2:

    1. 7 9
    2. 0 4 1 1 1
    3. 1 6 1 1 3
    4. 2 6 1 1 1
    5. 2 5 1 2 2
    6. 3 0 0 1 1
    7. 3 1 1 1 3
    8. 3 2 1 1 2
    9. 4 5 0 2 2
    10. 6 5 1 1 2
    11. 3 5

    Sample Output 2:

    Distance = 3; Time = 4: 3 -> 2 -> 5

    两次Dijkstra ,使用数组分别记录每条路径中每个结点的前驱来记录路径,最后先判断路径是否相等再输出:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. using namespace std;
    8. int n, m, v1, v2, t, D, T, s, d;
    9. int dis[510], Time[510], cnt[510];
    10. int ansd[510], anst[510], cntd, cntt;
    11. int pred[510], pret[510]; //记录前驱
    12. struct edge {
    13. int next, dis, time;
    14. };
    15. vectorg[510];
    16. bool vis[510];
    17. priority_queueint, int>, vectorint, int>>, greaterint, int>>>q;
    18. void dfs_d(int x) {
    19. if (x == s) {
    20. ansd[cntd++] = x;
    21. return;
    22. }
    23. dfs_d(pred[x]);
    24. ansd[cntd++] = x;
    25. }
    26. void dfs_t(int x) {
    27. if (x == s) {
    28. anst[cntt++] = x;
    29. return;
    30. }
    31. dfs_t(pret[x]);
    32. anst[cntt++] = x;
    33. }
    34. int main() {
    35. cin >> n >> m;
    36. for (int i = 0; i < m; i++) {
    37. cin >> v1 >> v2 >> t >> D >> T;
    38. edge e;
    39. e.next = v2;
    40. e.dis = D;
    41. e.time = T;
    42. g[v1].push_back(e);
    43. if (t == 0) {
    44. e.next = v1;
    45. g[v2].push_back(e);
    46. }
    47. }
    48. cin >> s >> d;
    49. memset(dis, 127, sizeof(dis));
    50. memset(Time, 127, sizeof(Time));
    51. dis[s] = 0;
    52. Time[s] = 0;
    53. q.push(make_pair(0, s));
    54. while (!q.empty()) {
    55. int u = q.top().second;
    56. q.pop();
    57. if (vis[u]) {
    58. continue;
    59. }
    60. vis[u] = 1;
    61. for (int i = 0; i < g[u].size(); i++) {
    62. int v = g[u][i].next;
    63. if (dis[v] > dis[u] + g[u][i].dis) {
    64. dis[v] = dis[u] + g[u][i].dis;
    65. Time[v] = Time[u] + g[u][i].time;
    66. pred[v] = u;
    67. q.push(make_pair(dis[v], v));
    68. } else if (dis[v] == dis[u] + g[u][i].dis) {
    69. if (Time[v] > Time[u] + g[u][i].time) {
    70. Time[v] = Time[u] + g[u][i].time;
    71. pred[v] = u;
    72. q.push(make_pair(dis[v], v));
    73. }
    74. }
    75. }
    76. }
    77. memset(Time, 127, sizeof(Time));
    78. memset(cnt, 127, sizeof(cnt));
    79. memset(vis, 0, sizeof(vis));
    80. Time[s] = 0;
    81. cnt[s] = 0;
    82. q.push(make_pair(0, s));
    83. while (!q.empty()) {
    84. int u = q.top().second;
    85. q.pop();
    86. if (vis[u]) {
    87. continue;
    88. }
    89. vis[u] = 1;
    90. for (int i = 0; i < g[u].size(); i++) {
    91. int v = g[u][i].next;
    92. if (Time[v] > Time[u] + g[u][i].time) {
    93. Time[v] = Time[u] + g[u][i].time;
    94. cnt[v] = cnt[u] + 1;
    95. pret[v] = u;
    96. q.push(make_pair(Time[v], v));
    97. } else if (Time[v] == Time[u] + g[u][i].time) {
    98. if (cnt[v] > cnt[u] + 1) {
    99. cnt[v] = cnt[u] + 1;
    100. pret[v] = u;
    101. q.push(make_pair(Time[v], v));
    102. }
    103. }
    104. }
    105. }
    106. dfs_d(d);
    107. dfs_t(d);
    108. bool flag = 0;
    109. if (cntd == cntt) {
    110. for (int i = 0; i < cntd; i++) {
    111. if (ansd[i] != anst[i]) {
    112. flag = 1;
    113. break;
    114. }
    115. }
    116. } else {
    117. flag = 1;
    118. }
    119. if (flag) {
    120. cout << "Distance = " << dis[d] << ": ";
    121. for (int i = 0; i < cntd; i++) {
    122. cout << ansd[i];
    123. if (i != cntd - 1) {
    124. cout << " -> ";
    125. }
    126. }
    127. cout << endl;
    128. cout << "Time = " << Time[d] << ": ";
    129. for (int i = 0; i < cntt; i++) {
    130. cout << anst[i];
    131. if (i != cntt - 1) {
    132. cout << " -> ";
    133. }
    134. }
    135. } else {
    136. cout << "Distance = " << dis[d] << "; Time = " << Time[d] << ": ";
    137. for (int i = 0; i < cntd; i++) {
    138. cout << ansd[i];
    139. if (i != cntd - 1) {
    140. cout << " -> ";
    141. }
    142. }
    143. }
    144. return 0;
    145. }

     

  • 相关阅读:
    JaveEE进阶----Spring Web MVC入门
    【建议收藏】Kafka 面试连环炮, 看看你能撑到哪一步?
    Qt 二维码生成与识别
    Linux学习之expect操作详解
    Mycat核心教程--Mycat 监控工具【四】
    sql解决连续登录问题变形-节假日过滤
    11 函数的极值和最大最小值
    C#编程题分享(2)
    tomcat 服务器
    Spring Boot Validation提示信息国际化配置
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/127459085