zoukankan      html  css  js  c++  java
  • 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 (<=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

    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<iostream>
     2 #include<vector>
     3 using namespace std;
     4 int n, m, st, ed;
     5 const int inf = 99999999;
     6 int cost[500][500], dis[500][500], vis[500], d[500];
     7 vector<int> pre[500], temp, path;
     8 int mincost=inf;
     9 void dfs(int node){
    10   temp.push_back(node);
    11   if(node==st){
    12     int tempcost=0;
    13     for(int j=0; j<temp.size()-1; j++) tempcost += cost[temp[j]][temp[j+1]];
    14     if(mincost>tempcost){
    15       mincost = tempcost;
    16       path = temp;
    17     }
    18     temp.pop_back();
    19     return;
    20   }
    21   for(int i=0; i<pre[node].size(); i++) dfs(pre[node][i]);
    22   temp.pop_back();
    23 }
    24 int main(){
    25   fill(dis[0], dis[0]+500*500, inf);
    26   fill(d, d+500, inf);
    27   fill(vis, vis+500, false);
    28   scanf("%d%d%d%d", &n, &m, &st, &ed);
    29   int i;
    30   for(i=0; i<m; i++){
    31     int a, b, ccost, ddis;
    32     scanf("%d%d%d%d", &a, &b, &ddis, &ccost);
    33     cost[a][b]=cost[b][a]=ccost;
    34     dis[a][b]=dis[b][a]=ddis;
    35   }
    36   d[st]=0;
    37   for(i=0; i<n; i++){
    38     int u=-1, minn=inf;
    39     for(int j=0; j<n; j++){
    40       if(!vis[j] && d[j]<minn){
    41         minn = d[j];
    42         u=j;
    43       }
    44     }
    45     if(u==-1) break;
    46     vis[u] = true;
    47     for(int v=0; v<n; v++){
    48       if(!vis[v] && dis[v][u]<inf){
    49         if(d[v]>dis[v][u]+d[u]){
    50           pre[v].clear();
    51           pre[v].push_back(u);
    52           d[v] = d[u] + dis[u][v];
    53         }else if(d[v]==dis[v][u]+d[u]){
    54           pre[v].push_back(u);
    55         }
    56       }
    57     }
    58   }
    59   dfs(ed);
    60   for(i=path.size()-1; i>=0; i--) printf("%d ", path[i]);
    61   printf("%d %d
    ", d[ed], mincost);
    62   return 0;
    63 }
    有疑惑或者更好的解决方法的朋友,可以联系我,大家一起探讨。qq:1546431565
  • 相关阅读:
    exe自启动的几种方式
    关于 CShellManager 的作用
    DLL 调用 对话框 以及 如何获取调用dll 应用程序(窗口程序)的窗口句柄
    VC++ 2010 创建高级Ribbon界面详解(4)
    HPU--1221 Fibonacci数列
    取一个数的前几位
    HDU--1875 畅通工程再续
    POJ--2485 Highways
    【模板】HDU--1233 畅通工程
    hdu--1856 More is better
  • 原文地址:https://www.cnblogs.com/mr-stn/p/9180174.html
Copyright © 2011-2022 走看看