zoukankan      html  css  js  c++  java
  • HDU 1385 Minimum Transport Cost (Dijstra 最短路)

    Minimum Transport Cost

    http://acm.hdu.edu.cn/showproblem.php?pid=1385

    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
     
     
    最后再提供两组测试数据:
    Sample Input

    4
    0 2 3 9
    2 0 1 5
    3 1 0 3
    9 5 3 1
    0 0 0 0
    1 4
    -1 -1

    4
    0 2 3 9
    2 0 1 5
    3 1 0 3
    9 5 3 1
    1 2 3 4
    1 4
    -1 -1

    Sample Output

    From 1 to 4 :
    Path: 1-->2-->3-->4
    Total cost : 6

    From 1 to 4 :
    Path: 1-->2-->4
    Total cost : 9

     

     解题思路:将最短路保存起来,最短路相同,比较字典序,输出字典序最小的那个方案,此题难点也是按字典序输出!

    具体方案见源代码!

    解题代码:

      1 // File Name: Minimum Transport Cost 1385.cpp
      2 // Author: sheng
      3 // Created Time: 2013年07月18日 星期四 23时36分58秒
      4 
      5 #include <stdio.h>
      6 #include <string.h>
      7 #include <iostream>
      8 using namespace std;
      9 
     10 const int max_n = 10002;
     11 const int INF = 0x3fffffff;
     12 int map[max_n][max_n], cos[max_n];
     13 int vis[max_n], dis[max_n];
     14 int cun[max_n];
     15 int bg, ed, n;
     16 
     17 int out(int x, int y, int bg)
     18 {
     19     if (x != bg)
     20         x = out (cun[x], x, bg);
     21     printf("%d%s", x, x == ed ? "
    " : "-->");
     22     return y;
     23 }
     24 
     25 int sort(int j, int k)
     26 {
     27     int path_1[max_n];
     28     int path_2[max_n];
     29     int len1 = 0, len2 = 0;
     30     path_1[len1 ++] = j;
     31     for (int i = k; ; i = cun[i])
     32     {
     33         path_1[len1 ++] = i;
     34         if (i == bg)
     35             break;
     36     }
     37     for (int i = j; ; i = cun[i])
     38     {
     39         path_2[len2 ++] = i;
     40         if (i == bg)
     41             break;
     42     }
     43     len1 --;
     44     len2 --;
     45     int len = len1 < len2 ? len1 : len2;
     46     for (int i = 0; i <= len; i ++)
     47     {
     48         if (path_1[len1 - i] < path_2[len2 - i])
     49             return 1;
     50         if (path_1[len1 - i] > path_2[len2 - i])
     51             return 0;
     52     }
     53     if (len1 < len2)
     54         return 1;
     55     return 0;
     56 
     57 }
     58 
     59 int main ()
     60 {
     61     while (~scanf ("%d", &n), n)
     62     {
     63         for (int i = 1; i <= n; i ++)
     64             for (int j = 1; j <= n; j ++)
     65                 scanf ("%d", &map[i][j]);
     66         for (int i = 1; i <= n; i ++)
     67             scanf ("%d", &cos[i]);
     68         int T = 1;
     69         while (scanf ("%d%d", &bg, &ed) && bg != -1 && ed != -1)
     70         {
     71             
     72             memset(vis, 0, sizeof (vis));
     73             for (int i = 1; i <= n; i ++)
     74             {
     75                 if (map[bg][i] != -1 && bg != i)
     76                 {
     77                     dis[i] = map[bg][i] + cos[i];
     78                     cun[i] = bg;
     79                 }
     80                 else  dis[i] = INF;
     81             }
     82             dis[bg] = 0;
     83             vis[bg] = 1;
     84             for (int i = 1; i <= n; i ++)
     85             {
     86                 int k;
     87                 int min = INF;
     88                 for (int j = 1; j <= n; j++)
     89                 {
     90                     if (!vis[j] && min > dis[j])
     91                     {
     92                         min = dis[j];
     93                         k = j;
     94                     }
     95                 }
     96                 vis[k] = 1;
     97                 for (int j = 1; j <= n; j ++)
     98                     if (!vis[j] && map[k][j] != -1)
     99                     {
    100                         if ( dis[j] > dis[k] + map[k][j] + cos[j])
    101                         {
    102                             cun[j] = k;
    103                             dis[j] = map[k][j] + dis[k] + cos[j];
    104                         }
    105                         else if (dis[j] == dis[k] + map[k][j] + cos[j])//花费相同时,寻找字典序最小的方案
    106                         {
    107                             if (sort(j, k))//比较字典序
    108                                 cun[j] = k;
    109                         }
    110                     }
    111 
    112             }
    113             printf ("From %d to %d :
    ", bg, ed);
    114             printf ("Path: ");
    115             out (ed, 0, bg);//输出路径
    116             printf ("Total cost : %d
    
    ", bg == ed ? 0 : dis[ed] - cos[ed]);
    117         }
    118     }
    119     return 0;
    120 }
  • 相关阅读:
    java--exceptions
    java-interface
    Java笔记
    memcpy
    const 关键字
    LeeCode整数 反转
    函数调用运算符笔记
    cvCreateImage
    c++继承笔记1
    虚拟机下的debian无法登陆
  • 原文地址:https://www.cnblogs.com/shengshouzhaixing/p/3200662.html
Copyright © 2011-2022 走看看