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. [转]pro*c/c++编译错误 ” error: sqlca.h: No such file or directory “ 的解决办法

    $ gcc -o test test.c 出现错误:error: sqlca.h: No such file or directory [解决方法]知道 sqlca.h 在 $ORACLE_HOME/ ...

  2. bootstrap-table-master

    http://bootstrap-table.wenzhixin.net.cn/getting-started/ 1.安装bower 2. 3.编译css and js 以上就编译完了boostrap ...

  3. Android--Fragment的懒加载

    我们都知道,fragment放在viewPager里面,viewpager会帮我们预先加载一个,但是当我们要看fragment里面的内容时,我们也许只会去看第一个,不会去看第二个,如果这时候不去实现f ...

  4. DataReader 链接关闭解惑篇

    不管是啥xxDataReader,都是继承DataReader实现的,所以是有共性的,因此标题就以DataReader为题了. 情况一:DataReader 默认链接不关闭 static void M ...

  5. ios中如何计算(页数,行数,等等的算法)

    页数 = (总个数 + 每页最大显示个数 - 1) / 每页显示最大的个数

  6. sychronized面试问题浅析

    先说下面试吧,整体来说基础准备好点,简历别太假,然后回答起来实事求是,表现自然的点基本上都没问题吧(针对初级职位,记得有个hr说过对于新人基础扎实和为人真诚是最关键的),两天时间跑起来挺累,反而觉得面 ...

  7. python小算法(二)

    有两个序列a,b,大小都为n,序列元素的值任意整形数,无序: 要求:通过交换a,b中的元素,使[序列a元素的和]与[序列b元素的和]之间的差最小.(华为面试) def diff(sorted_list ...

  8. iOS学习之Object-C语言简单的通讯录管理系统

    用这几天学的OC的知识,写了一个实现简单功能的通讯录管理系统,在这里分享给大家: 通讯录管理系统 *  需求: 1.定义联系人类Contact.实例变量:姓名(拼音,首字母大写).性别.电话号码.住址 ...

  9. com.Goods.ForEach

    com.Goods.ForEach(g => { g.TransactionPrice = getUnitPriceByProductId(g.ProductID); g.ExpressMone ...

  10. EntityFramwork(1) 源地址https://msdn.microsoft.com/zh-cn/data/jj193542

    1.创建应用程序 简单起见,我们将构建一个使用 Code First 执行数据访问的基本控制台应用程序. 打开 Visual Studio "文件"->"新建&qu ...