zoukankan      html  css  js  c++  java
  • 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 (<=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<cstdio>
     2 #include<iostream>
     3 #include<vector>
     4 #include<algorithm>
     5 using namespace std;
     6 int N, M, S, D;
     7 const int INF = 100000000;
     8 int G[500][500], cost[500][500], visit[500], dst[500];
     9 vector<int> pre[500];
    10 void dijkstra(int s){
    11     fill(visit, visit + 500, 0);
    12     fill(dst, dst + 500, INF);
    13     dst[s] = 0;
    14     for(int i = 0; i < N; i++){
    15         int u = -1, minLen = INF;
    16         for(int j = 0; j < N; j++){        
    17             if(visit[j] == 0 && dst[j] < minLen){
    18                 minLen = dst[j];
    19                 u = j;
    20             }
    21         }
    22         if(u == -1){
    23             return;
    24         }
    25         visit[u] = 1;
    26         for(int j = 0; j < N; j++){
    27             if(visit[j] == 0 && G[u][j] != INF){
    28                 if(G[u][j] + dst[u] < dst[j]){
    29                     dst[j] = G[u][j] + dst[u];
    30                     pre[j].clear();
    31                     pre[j].push_back(u);
    32                 }else if(G[u][j] + dst[u] == dst[j]){
    33                     dst[j] = G[u][j] + dst[u];
    34                     pre[j].push_back(u);
    35                 }            
    36             }
    37         }
    38     }    
    39 }
    40 vector<int> tempPath, ans;
    41 int minCost = INF;
    42 void DFS(int d){
    43     tempPath.push_back(d);
    44     if(d == S){
    45         int tempCost = 0;
    46         for(int i = tempPath.size() - 1; i > 0; i--){
    47             tempCost += cost[tempPath[i]][tempPath[i - 1]];
    48         }
    49         if(tempCost < minCost){
    50             minCost = tempCost;
    51             ans = tempPath;
    52         }
    53         tempPath.pop_back();
    54         return;
    55     }
    56     int len = pre[d].size();
    57     for(int i =0; i < len; i++){
    58         DFS(pre[d][i]);
    59     }
    60     tempPath.pop_back();
    61 }
    62 int main(){
    63     fill(G[0], G[0] + 500*500, INF);
    64     scanf("%d%d%d%d", &N, &M, &S, &D);
    65     int tempA, tempB, tempD, tempC;
    66     for(int i = 0; i < M; i++){
    67         scanf("%d%d%d%d", &tempA, &tempB, &tempD, &tempC);
    68         G[tempA][tempB] = G[tempB][tempA] = tempD;
    69         cost[tempA][tempB] = cost[tempB][tempA] = tempC;
    70     }
    71     dijkstra(S);
    72     DFS(D);
    73     for(int i = ans.size() - 1; i >= 0; i--){
    74         printf("%d ", ans[i]);
    75     }
    76     printf("%d %d", dst[D], minCost);
    77     cin >> N;
    78     return 0;
    79 }
    View Code

    总结:

    1、题意:求出最短路径,如果有多条则求出花费的边权之和最短的一条。本题可采用dijkstra专门搜索最短路,将其存入pre数组。再用深搜遍历pre,计算、比较cost的花费并保存最优路径。

    2、迪杰斯特拉:注意每次选择一个未优化的且距离最短的节点作为u,并将其visit设为1。注意在选择更新的节点也要是未被优化过的节点。 注意别忘记更新dst。

    3、深搜注意push和pop的位置和时机。

  • 相关阅读:
    编码问题
    apache 2.4 httpd 2.4.6 反向代理后端的服务为HTTPS https 基于centos7
    ucore系统 eclipse-cdt实验环境准备
    openshift v1.5 不能登录system:admin 问题
    filebeat v6.3 如何增加ip 字段
    filebeat v6.3 多行合并的步骤 多个表达式同时匹配
    如何一步步使用国内yum源一键安装openstack-ocata版本基于centos7
    windows 定时删除N天前日志脚本
    logrotate 如何执行日志按照大小切分
    virtualbox 基于nat模式搭建局域网并且和宿主机通信
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8557032.html
Copyright © 2011-2022 走看看