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

 

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

具体方案见源代码!

解题代码:

 // File Name: Minimum Transport Cost 1385.cpp
// Author: sheng
// Created Time: 2013年07月18日 星期四 23时36分58秒 #include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std; const int max_n = ;
const int INF = 0x3fffffff;
int map[max_n][max_n], cos[max_n];
int vis[max_n], dis[max_n];
int cun[max_n];
int bg, ed, n; int out(int x, int y, int bg)
{
if (x != bg)
x = out (cun[x], x, bg);
printf("%d%s", x, x == ed ? "\n" : "-->");
return y;
} int sort(int j, int k)
{
int path_1[max_n];
int path_2[max_n];
int len1 = , len2 = ;
path_1[len1 ++] = j;
for (int i = k; ; i = cun[i])
{
path_1[len1 ++] = i;
if (i == bg)
break;
}
for (int i = j; ; i = cun[i])
{
path_2[len2 ++] = i;
if (i == bg)
break;
}
len1 --;
len2 --;
int len = len1 < len2 ? len1 : len2;
for (int i = ; i <= len; i ++)
{
if (path_1[len1 - i] < path_2[len2 - i])
return ;
if (path_1[len1 - i] > path_2[len2 - i])
return ;
}
if (len1 < len2)
return ;
return ; } int main ()
{
while (~scanf ("%d", &n), n)
{
for (int i = ; i <= n; i ++)
for (int j = ; j <= n; j ++)
scanf ("%d", &map[i][j]);
for (int i = ; i <= n; i ++)
scanf ("%d", &cos[i]);
int T = ;
while (scanf ("%d%d", &bg, &ed) && bg != - && ed != -)
{ memset(vis, , sizeof (vis));
for (int i = ; i <= n; i ++)
{
if (map[bg][i] != - && bg != i)
{
dis[i] = map[bg][i] + cos[i];
cun[i] = bg;
}
else dis[i] = INF;
}
dis[bg] = ;
vis[bg] = ;
for (int i = ; i <= n; i ++)
{
int k;
int min = INF;
for (int j = ; j <= n; j++)
{
if (!vis[j] && min > dis[j])
{
min = dis[j];
k = j;
}
}
vis[k] = ;
for (int j = ; j <= n; j ++)
if (!vis[j] && map[k][j] != -)
{
if ( dis[j] > dis[k] + map[k][j] + cos[j])
{
cun[j] = k;
dis[j] = map[k][j] + dis[k] + cos[j];
}
else if (dis[j] == dis[k] + map[k][j] + cos[j])//花费相同时,寻找字典序最小的方案
{
if (sort(j, k))//比较字典序
cun[j] = k;
}
} }
printf ("From %d to %d :\n", bg, ed);
printf ("Path: ");
out (ed, , bg);//输出路径
printf ("Total cost : %d\n\n", bg == ed ? : dis[ed] - cos[ed]);
}
}
return ;
}

HDU 1385 Minimum Transport Cost (Dijstra 最短路)的更多相关文章

  1. HDU 1385 Minimum Transport Cost (最短路,并输出路径)

    题意:给你n个城市,一些城市之间会有一些道路,有边权.并且每个城市都会有一些费用. 然后你一些起点和终点,问你从起点到终点最少需要多少路途. 除了起点和终点,最短路的图中的每个城市的费用都要加上. 思 ...

  2. hdu 1385 Minimum Transport Cost(floyd &amp;&amp; 记录路径)

    Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  3. hdu 1385 Minimum Transport Cost (Floyd)

    Minimum Transport CostTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  4. HDU 1385 Minimum Transport Cost (输出字典序最小路径)【最短路】

    <题目链接> 题目大意:给你一张图,有n个点,每个点都有需要缴的税,两个直接相连点之间的道路也有需要花费的费用.现在进行多次询问,给定起点和终点,输出给定起点和终点之间最少花费是多少,并且 ...

  5. hdu 1385 Minimum Transport Cost (floyd算法)

    貌似···················· 这个算法深的东西还是很不熟悉!继续学习!!!! ++++++++++++++++++++++++++++ ======================== ...

  6. hdu 1385 Minimum Transport Cost

    http://acm.hdu.edu.cn/showproblem.php?pid=1385 #include <cstdio> #include <cstring> #inc ...

  7. HDU 1385 Minimum Transport Cost( Floyd + 记录路径 )

    链接:传送门 题意:有 n 个城市,从城市 i 到城市 j 需要话费 Aij ,当穿越城市 i 的时候还需要话费额外的 Bi ( 起点终点两个城市不算穿越 ),给出 n × n 大小的城市关系图,-1 ...

  8. HDU 1385 Minimum Transport Cost 最短路径题解

    本题就是使用Floyd算法求全部路径的最短路径,并且须要保存路径,并且更进一步须要依照字典顺序输出结果. 还是有一定难度的. Floyd有一种非常巧妙的记录数据的方法,大多都是使用这种方法记录数据的. ...

  9. 【HDOJ】1385 Minimum Transport Cost

    Floyd.注意字典序!!! #include <stdio.h> #include <string.h> #define MAXNUM 55 #define INF 0x1f ...

随机推荐

  1. Nginx服务器架构简析

    一.Nginx的模块化 模块化结构的思想是一个很久的概念,但也正是成熟的思想造就了Nginx的巨大优越性. 我们知道Nginx从总体上来讲是有许多个模块构成的.习惯将Nginx分为5大模块分别为:核心 ...

  2. 在Linux上安装JDK7

    查看是否安装了JDK 如果安装完毕后,jdk版本不是当前所安装的,则需要卸载之前linux自带的jdk版本,因为安装Redhat9后默认安装了jdk, 可是默认安装的jdk1.4版本比较老,所以需要先 ...

  3. hdu 1622 Trees on the level

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1622 小白书上的题... #include<algorithm> #include< ...

  4. hdu 2648 Shopping

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2648 纯暴力的方法T_T... 如下: #include<cstdio> #include ...

  5. JavaScript美术馆进化史

    内容选自<<JavaScript DOM 编程艺术>>第4-6章,跟着作者一起见证美术馆的进化吧. 先放效果图,然后一步步做出每个效果.每个效果都有它实用的地方,且知道过程可以 ...

  6. 基于.net mvc的校友录(五、web.config对的配置以及filter实现的权限控制)

    web.config配置文件 此文件是整个系统的配置中心,它告诉iis服务器本网站需要哪些运行时环境,需要哪些环境,将要进行哪些操作,开发人员也会将一个常量性的数据放在此配置中,以备系统全局调用.此文 ...

  7. OA Framework - How to Find the Correct Version of JDeveloper to Use with E-Business Suite 11i or Release 12.x (Doc ID 416708.1)

    APPLIES TO: Oracle Applications Framework - Version 11.5.10.0 to 12.2.2 [Release 11.5.10 to 12.2] In ...

  8. 远航1617团队alpha版本分数分配与人员调动

    一.根据项目开始初期的分数分配要求及项目发布后大家的讨论,我们对组内成员的分数分配如下: 刘昊岩 20.5 周  萱 20.0 林谋武 19.0 杨  帆 18.5 高小洲 21.0 谢勤政 21.5 ...

  9. Careercup - Google面试题 - 5377673471721472

    2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...

  10. 【Valid Number】cpp

    题目: Validate if a given string is numeric. Some examples:"0" => true" 0.1 " = ...