zoukankan      html  css  js  c++  java
  • HD1385Minimum Transport Cost(Floyd + 输出路径)

    Minimum Transport Cost

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 9109    Accepted Submission(s): 2405


    Problem Description
    These are N cities in Spring country. Between each pair of cities there may be one transportation track or none. Now there is some cargo that should be delivered from one city to another. The transportation fee consists of two parts:
    The cost of the transportation on the path between these cities, and

    a certain tax which will be charged whenever any cargo passing through one city, except for the source and the destination cities.

    You must write a program to find the route which has the minimum cost.
     

     

    Input
    First is N, number of cities. N = 0 indicates the end of input.

    The data of path cost, city tax, source and destination cities are given in the input, which is of the form:

    a11 a12 ... a1N
    a21 a22 ... a2N
    ...............
    aN1 aN2 ... aNN
    b1 b2 ... bN

    c d
    e f
    ...
    g h

    where aij is the transport cost from city i to city j, aij = -1 indicates there is no direct path between city i and city j. bi represents the tax of passing through city i. And the cargo is to be delivered from city c to city d, city e to city f, ..., and g = h = -1. You must output the sequence of cities passed by and the total cost which is of the form:
     

     

    Output
    From c to d :
    Path: c-->c1-->......-->ck-->d
    Total cost : ......
    ......

    From e to f :
    Path: e-->e1-->..........-->ek-->f
    Total cost : ......

    Note: if there are more minimal paths, output the lexically smallest one. Print a blank line after each test case.

     

     

    Sample Input
    5 0 3 22 -1 4 3 0 5 -1 -1 22 5 0 9 20 -1 -1 9 0 4 4 -1 20 4 0 5 17 8 3 1 1 3 3 5 2 4 -1 -1 0
     

     

    Sample Output
    From 1 to 3 : Path: 1-->5-->4-->3 Total cost : 21 From 3 to 5 : Path: 3-->4-->5 Total cost : 16 From 2 to 4 : Path: 2-->1-->5-->4 Total cost : 17
     
    题意:输入N个城市,然后接下来N*N表示相互之间的连接路径,接下来输入两个城市求最短路并输出路径
    分析:首先用Floyd求任意两点的距离,然后在更新的时候更新path[][]
    path[i][j]用来保存 i --> j 的最短路径中 i 的最优后驱(即最近),在Floyd三重循环时,一直更新path。  
    在输出路径注意的就是相同的城市直接输入这个城市就可以了,这个问题上浪费了一个多小时。
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <string.h>
     6 using namespace std;
     7 const int INF = 1 << 25;
     8 const int MAX = 1000;
     9 int n,path[MAX][MAX],dist[MAX][MAX],tax[MAX];
    10 /path[i][j]用来保存 i --> j 的最短路径中 i 的最优后驱(即最近),在Floyd三重循环时,一直更新path。  
    11 void Floyd(int n)
    12 {
    13     for(int i = 1; i <= n; i++)
    14     {
    15         for(int j = 1; j <= n; j++)
    16             path[i][j] = j;
    17     }
    18 
    19     for(int k = 1; k <= n; k++)
    20     {
    21         for(int i = 1; i <= n; i++)
    22         {
    23             for(int j = 1; j <= n; j++)
    24             {
    25                 if(dist[i][k] < INF && dist[k][j] < INF)
    26                 {
    27                     int temp = dist[i][k] + dist[k][j] + tax[k];
    28                     if(temp < dist[i][j])
    29                     {
    30                         dist[i][j] = temp;
    31                         path[i][j] = path[i][k];
    32                     }
    33                     else if(temp == dist[i][j])   //因为是按照字典序输出路径因此当距离相等时,取路径中最小的那个
    34                     {
    35                         if(path[i][j] > path[i][k])
    36                             path[i][j] = path[i][k];
    37                     }
    38                 }
    39             }
    40         }
    41     }
    42 
    43 
    44 }
    45 int main()
    46 {
    47     while(scanf("%d", &n) != EOF && n)
    48     {
    49         for(int i = 1; i <= n; i++)
    50         {
    51             for(int j = 1; j <= n; j++)
    52             {
    53                 scanf("%d", &dist[i][j]);
    54                 if(dist[i][j] == -1)
    55                     dist[i][j] = INF;
    56             }
    57         }
    58         for(int i = 1; i <= n; i++)
    59             scanf("%d", &tax[i]);
    60         Floyd(n);
    61         int start,goal;
    62         while(scanf("%d%d", &start,&goal) != EOF)
    63         {
    64             if(start == -1 && goal == -1)
    65                 break;
    66             printf("From %d to %d :
    ",start,goal);
    67            
    68             int temp = start;
    69             printf("Path: %d",start);
    70             while(temp != goal)
    71             {
    72                 printf("-->%d", path[temp][goal]);
    73                 temp = path[temp][goal];
    74             }
    75             printf("
    ");
    76              /*
    77             int temp = path[start][goal];  //就是这个一直把start和goal当成不会重复的了,脑子啊~
    78             printf("Path: %d",start);
    79             while(temp != goal)
    80             {
    81                 printf("-->%d",temp);
    82                 temp = path[temp][goal];
    83             }
    84             printf("-->%d
    ",goal);  
    85             */
    86             printf("Total cost : %d
    ",dist[start][goal]);
    87             printf("
    ");
    88         }
    89     }
    90     return 0;
    91 }
     
  • 相关阅读:
    第9章 线程编程(7)_线程同步4:信号量
    第9章 线程编程(6)_线程同步3:条件变量
    第9章 线程编程(5)_线程同步2:读写锁
    第9章 线程编程(4)_线程同步1:互斥锁
    第9章 线程编程(3)_线程清理和控制函数
    第9章 线程编程(2)_线程创建和终止
    第9章 线程编程(1)_线程概念和线程标识
    第8章 信号(6)_贯穿案例2:mini shell(3)
    第8章 信号(5)_信号集和信号屏蔽
    第8章 信号(4)_中断的系统调用和函数可重入性
  • 原文地址:https://www.cnblogs.com/zhaopAC/p/4990040.html
Copyright © 2011-2022 走看看