zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    记录一个bug关于radio的
    at notFoundError …@cross-spawnlibenoent.js: 报错解决
    关于目前高清屏幕150%适配需要注意的样式写法
    记录一个css样式覆盖的问题。
    video.js使用详解(转载)
    记录一次node版本更新的问题,node的exe版本安装不上去。
    webstorm左侧导航栏背景变成黄色的,文件搜索,定位等一系列功能都不好用了?
    SCRIPT5007: 无法获取未定义或 null 引用的属性“xxx”
    VMWare中安装CentOS6.6不能上网的解决办法
    Unity出现 error building player exception android (invocation failed)
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/11075075.html
Copyright © 2011-2022 走看看