• 1030 Travel Plan


    A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.

    Input Specification:

    Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (≤500) is the number of cities (and hence the cities are numbered from 0 to N−1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:

    City1 City2 Distance Cost
    

    where the numbers are all integers no more than 500, and are separated by a space.

    Output Specification:

    For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.

    Sample Input:

    1. 4 5 0 3
    2. 0 1 1 20
    3. 1 3 2 30
    4. 0 3 4 10
    5. 0 2 2 20
    6. 2 3 1 20

    Sample Output:

    0 2 3 3 40

    注意存储路径时使用的数组pre存储的应该是前驱,应该用dfs遍历输出,否则测试点一不通过。 

    1. #include <iostream>
    2. #include <queue>
    3. #include <vector>
    4. #include <algorithm>
    5. #include <cstring>
    6. using namespace std;
    7. struct edge {
    8. int next, cost, dis;
    9. };
    10. vector<edge>g[1000];
    11. int n, m, s, d, dis[1000], cost[1000];
    12. int pre[1000];//记录最短路径 前驱
    13. bool vis[1000];
    14. priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>q;
    15. void dfs(int x) {
    16. if (x == s) {
    17. cout << x << ' ';
    18. return;
    19. }
    20. dfs(pre[x]);
    21. cout << x << ' ';
    22. }
    23. int main() {
    24. cin >> n >> m >> s >> d;
    25. for (int i = 1; i <= m; i++) {
    26. int u, v;
    27. cin >> u >> v;
    28. edge e;
    29. cin >> e.dis >> e.cost;
    30. e.next = v;
    31. g[u].push_back(e);
    32. e.next = u;
    33. g[v].push_back(e);
    34. }
    35. memset(dis, 127, sizeof(dis));
    36. memset(cost, 127, sizeof(cost));
    37. dis[s] = 0;
    38. cost[s] = 0;
    39. q.push(make_pair(0, s));
    40. while (!q.empty()) {
    41. int u = q.top().second;
    42. q.pop();
    43. if (vis[u]) {
    44. continue;
    45. }
    46. vis[u] = 1;
    47. for (int i = 0; i < g[u].size(); i++) {
    48. int v = g[u][i].next;
    49. if (dis[v] > dis[u] + g[u][i].dis) {
    50. dis[v] = dis[u] + g[u][i].dis;
    51. cost[v] = cost[u] + g[u][i].cost;
    52. pre[v] = u;
    53. q.push(make_pair(dis[v], v));
    54. } else if (dis[v] == dis[u] + g[u][i].dis) {
    55. if (cost[v] > cost[u] + g[u][i].cost) {
    56. cost[v] = cost[u] + g[u][i].cost;
    57. pre[v] = u;
    58. q.push(make_pair(dis[v], v));
    59. }
    60. }
    61. }
    62. }
    63. dfs(d);
    64. cout << dis[d] << ' ' << cost[d];
    65. return 0;
    66. }

     

  • 相关阅读:
    Go 程序的init函数在什么时候执行
    优化代码 —— 减少 if - else
    Go语言快速入门笔记
    Linux·中断函数
    3、Nginx 常用的命令和配置文件
    1022 D进制的A+B
    2024全新版视频短剧SAAS系统/影视短剧小程序/短剧APP小程序源码
    通俗易懂:窗口函数 | 全是案例
    一比一还原axios源码(五)—— 拦截器
    动态规划之01背包问题
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/125471745