• 1030 Travel Plan (30 分)


    1030 Travel Plan (30 分)
     

    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 (≤) is the number of cities (and hence the cities are numbered from 0 to N1); M is the number of highways; Sand 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:

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

    Sample Output:

    0 2 3 3 40

    最短路的板子题算是。
     1 #include <bits/stdc++.h>
     2 #define N 600
     3 #define inf 0x3f3f3f3f
     4 using namespace std;
     5 int n, m, s, d;
     6 struct Node{
     7     int to, val, cost;
     8     friend bool operator<(const Node &a, const Node &b){
     9         if(a.val == b.val)
    10             return a.cost > b.cost;
    11         return a.val > b.val;
    12     }
    13 };
    14 stack<int> st;
    15 vector<Node> v[N];
    16 int value[N],vis[N];
    17 int path[N];
    18 int fa[N];
    19 int min_val = inf, min_cost = inf;
    20 priority_queue<Node> q;
    21 void dij(int x){
    22     Node node;
    23     node.to = x, node.val = 0, node.cost = 0;
    24     q.push(node);
    25     while(!q.empty()){
    26         node = q.top();
    27         q.pop();
    28         if(vis[node.to])
    29             continue;
    30         vis[node.to] = 1;
    31         value[node.to] = min(value[node.to], node.val);
    32         for(int i = 0 ; i < v[node.to].size(); i++){
    33             Node ans = v[node.to][i];
    34             ans.val += value[node.to];
    35             ans.cost += node.cost;
    36             if(ans.val <= value[ans.to]){
    37                 value[ans.to] = ans.val;
    38                 path[ans.to] = node.to;
    39             }
    40             if(ans.to == d){
    41                 if(ans.val < min_val){
    42                     min_val = ans.val;
    43                     min_cost = ans.cost;
    44                     for(int i = 0 ; i <= n; i++){
    45                         fa[i] = path[i];
    46                     }
    47                 }else if(ans.val == min_val){
    48                     if(ans.cost < min_cost){
    49                         min_cost = ans.cost;
    50                         for(int i = 0 ; i <= n; i++){
    51                             fa[i] = path[i];
    52                         }
    53                     }
    54                 }
    55             }
    56             q.push(ans);
    57         }
    58     }
    59 
    60 }
    61 
    62 int main(){
    63     cin >> n >> m >> s >> d;
    64     int w,x,y,z;
    65     for(int i = 0; i < m; i++){
    66         cin >> x >> y >> z >> w;
    67         v[x].push_back({y,z,w});
    68         v[y].push_back({x,z,w});
    69     }
    70     memset(value, inf, sizeof(value));
    71     dij(s);
    72     while(d != s){
    73         st.push(d);
    74         d = fa[d];
    75     }
    76     cout << s;
    77     while(!st.empty()){
    78         cout <<" "<<st.top();
    79         st.pop();
    80     }
    81     cout <<" " <<min_val<<" "<<min_cost<<endl;
    82     return 0;
    83 }
  • 相关阅读:
    iOS:真机调试
    iOS:MBProgressHUD的基本使用
    CocoaPods安装小步骤
    PictureBox 双缓冲防止闪屏
    两招小办法对付宝宝发烧、咳嗽。超级管用哈
    (转)经纬度坐标转换为屏幕坐标
    解决eclipse不识别Android手机的问题
    利用FFmpeg将RTSP转码成RTMP发布在RED5
    vlc做转发的命令
    字符编码
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/11075075.html
Copyright © 2020-2023  润新知