• POJ 1385


    Minimum Transport Cost

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


    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
     1 //转载 
     2 //路径存储的是前一个点的后继 
     3 #include <iostream>
     4 #include <stdio.h>
     5 #include <memory.h>
     6 #include <queue>
     7 using namespace std;
     8 const int INF = 0x7fffff;//并不是整形最大值,防止溢出 
     9 const int N = 10000;
    10 int map[N][N], tax[N], path[N][N];
    11 int n;
    12 void init()
    13 {
    14     int i, j;
    15     for(i = 1; i <= n; i++)
    16         for(j = 1; j <= n; j++)
    17             if(i == j) map[i][j] = 0;
    18             else map[i][j] = INF;
    19 }
    20 void input()
    21 {
    22     int i, j, k;
    23     for(i = 1; i <= n; i++)
    24         for(j = 1; j <= n; j++)
    25         {
    26             scanf("%d", &k);
    27             if(k != -1) map[i][j] = k;
    28             path[i][j] = j;
    29         }
    30     for(i = 1; i <= n; i++)
    31         scanf("%d", &tax[i]);
    32 }
    33 void floyd()
    34 {
    35     int i, j, k, len;
    36     for(k = 1; k <= n; k++)
    37     {
    38         for(i = 1; i <= n; i++)
    39         {
    40             for(j = 1; j <= n; j++)
    41             {
    42                 len = map[i][k] + map[k][j] + tax[k];
    43                 if(map[i][j] > len)
    44                 {
    45                     map[i][j] = len;
    46                     path[i][j] = path[i][k];    //标记到该点的前一个点
    47                 }
    48                 else if(len == map[i][j])   //若距离相同
    49                 {
    50                     if(path[i][j] > path[i][k]) //判断是否为字典顺序
    51                         path[i][j] = path[i][k];
    52                 }
    53             }
    54         }
    55     }
    56 }
    57 void output()
    58 {
    59     int i, j, k;
    60     while(scanf("%d %d", &i, &j))
    61     {
    62         if(i == -1 && j == -1) break;
    63         printf("From %d to %d :\n", i, j);
    64         printf("Path: %d", i);
    65         k = i;
    66         while(k != j)   //输出路径从起点直至终点
    67         {
    68             printf("-->%d", path[k][j]);
    69             k = path[k][j];
    70         }
    71         printf("\n");
    72         printf("Total cost : %d\n\n", map[i][j]);
    73     }
    74 }
    75 int main()
    76 {
    77     while(scanf("%d", &n), n)
    78     {
    79         init();
    80         input();
    81         floyd();
    82         output();
    83     }
    84 
    85     return 0;
    86 }
    87 /*
    88 C语言,二维数组申请 
    89 int **map;
    90 *map = (int *)malloc(sizeof(int)*(city));
    91 for(i=0;i<city;i++)
    92     map[i] = (int *)malloc(sizeof(int)*city);
    93 */
  • 相关阅读:
    音视频入门-15-手动生成一张JPEG图片
    音视频入门-14-JPEG文件格式详解
    音视频入门-13-使用开源库生成PNG图片
    微信小程序笔记
    JS找到嵌套在iframe内联框架中的table表头元素
    京东 PC 首页 2019 改版前端操作总结
    web前端开发中各种图片引入方式及其优缺点
    10个面向前端开发人员的安全提示
    HTML/CSS switch开关 (包括JS控制checked选择)
    Echarts常用属性含义
  • 原文地址:https://www.cnblogs.com/hxsyl/p/2649665.html
Copyright © 2020-2023  润新知