zoukankan      html  css  js  c++  java
  • PAT甲级——A1030 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 (≤) is the number of cities (and hence the cities are numbered from 0 to N1); Mis 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 #define inf 999999999
     5 //使用dijkstra
     6 int N, M, S, D;
     7 vector<int>tempPath, path;
     8 struct Node
     9 {
    10     int dis = inf, w = inf;
    11 }node;
    12 int minW = inf;
    13 void DFS(vector<vector<Node>>&city, vector<vector<int>>&father, int k)
    14 {
    15     tempPath.push_back(k);
    16     if (k == S)
    17     {
    18         int tempW = 0;
    19         for (int i = tempPath.size() - 1; i > 0; --i)
    20             tempW += city[tempPath[i]][tempPath[i - 1]].w;
    21         if (tempW < minW)
    22         {
    23             minW = tempW;
    24             path = tempPath;
    25         }
    26         tempPath.pop_back();
    27         return;
    28     }
    29     for (int i = 0; i < father[k].size(); ++i)
    30         DFS(city, father, father[k][i]);
    31     tempPath.pop_back();
    32 }
    33 int main()
    34 {
    35     cin >> N >> M >> S >> D;
    36     vector<vector<Node>>city(N, vector<Node>(N, node));
    37     vector<vector<int>>father(N, vector<int>(1, S));
    38     for (int i = 0; i < M; ++i)
    39     {
    40         int a, b;
    41         cin >> a >> b >> node.dis >> node.w;
    42         city[a][b] = city[b][a] = node;
    43     }    
    44     vector<int>dis(N , inf);
    45     vector<bool>visit(N, false);
    46     dis[S] = 0;
    47     //Dijkstra
    48     for (int i = 0; i < N; ++i)
    49     {
    50         int index = -1, minDis = inf;
    51         for (int j = 0; j < N; ++j)
    52         {
    53             if (visit[j]== false && minDis > dis[j])
    54             {
    55                 index = j;
    56                 minDis = dis[j];
    57             }
    58         }
    59         if (index == -1)break;
    60         visit[index] = true;
    61         for (int j = 0; j < N; ++j)
    62         {
    63             if (visit[j] == false && city[index][j].dis < inf)
    64             {
    65                 if (dis[j] > dis[index] + city[index][j].dis)
    66                 {
    67                     dis[j] = dis[index] + city[index][j].dis;
    68                     father[j][0] = index;
    69                 }
    70                 else if(dis[j] == dis[index] + city[index][j].dis)
    71                     father[j].push_back(index);
    72             }
    73         }
    74     }
    75     DFS(city, father, D);
    76     for (int i = path.size() - 1; i >= 0; --i)
    77         cout << path[i] << " ";
    78     cout << dis[D] << " " << minW;
    79     return 0;
    80 }
  • 相关阅读:
    汉诺塔解法解析
    scrapy 集成到 django(三)
    scrapy 集成到 django(二)
    scrapy 集成到 django(一)
    日记-2017-7-26-javascript
    日记-2017-7-25-django/admin-Levenshtein
    日记-2017-7-24-cp-css-django/media
    二叉树 4 种排序方式
    归并排序 / 快排
    django-import-export 插件
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11254683.html
Copyright © 2011-2022 走看看