HDU 1385 Minimum Transport Cost (Dijstra 最短路)
Minimum Transport Cost
http://acm.hdu.edu.cn/showproblem.php?pid=1385
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.
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:
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.
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 最短路)的更多相关文章
- HDU 1385 Minimum Transport Cost (最短路,并输出路径)
题意:给你n个城市,一些城市之间会有一些道路,有边权.并且每个城市都会有一些费用. 然后你一些起点和终点,问你从起点到终点最少需要多少路途. 除了起点和终点,最短路的图中的每个城市的费用都要加上. 思 ...
- hdu 1385 Minimum Transport Cost(floyd && 记录路径)
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- hdu 1385 Minimum Transport Cost (Floyd)
Minimum Transport CostTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- HDU 1385 Minimum Transport Cost (输出字典序最小路径)【最短路】
<题目链接> 题目大意:给你一张图,有n个点,每个点都有需要缴的税,两个直接相连点之间的道路也有需要花费的费用.现在进行多次询问,给定起点和终点,输出给定起点和终点之间最少花费是多少,并且 ...
- hdu 1385 Minimum Transport Cost (floyd算法)
貌似···················· 这个算法深的东西还是很不熟悉!继续学习!!!! ++++++++++++++++++++++++++++ ======================== ...
- hdu 1385 Minimum Transport Cost
http://acm.hdu.edu.cn/showproblem.php?pid=1385 #include <cstdio> #include <cstring> #inc ...
- HDU 1385 Minimum Transport Cost( Floyd + 记录路径 )
链接:传送门 题意:有 n 个城市,从城市 i 到城市 j 需要话费 Aij ,当穿越城市 i 的时候还需要话费额外的 Bi ( 起点终点两个城市不算穿越 ),给出 n × n 大小的城市关系图,-1 ...
- HDU 1385 Minimum Transport Cost 最短路径题解
本题就是使用Floyd算法求全部路径的最短路径,并且须要保存路径,并且更进一步须要依照字典顺序输出结果. 还是有一定难度的. Floyd有一种非常巧妙的记录数据的方法,大多都是使用这种方法记录数据的. ...
- 【HDOJ】1385 Minimum Transport Cost
Floyd.注意字典序!!! #include <stdio.h> #include <string.h> #define MAXNUM 55 #define INF 0x1f ...
随机推荐
- hdu 5281 Senior's Gun
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5281 Senior's Gun Description Xuejiejie is a beautifu ...
- hdu 3282 Running Median
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3282 Running Median Description For this problem, you ...
- 微软开放技术(中国)携 CKAN 和 OData 技术引入基于 Azure 的开放数据平台
今天,微软开放技术(中国)通过微软公有云Azure引入一个全新的开放数据平台,该平台基于开源数据门户解决方案 CKAN,以及由微软开放技术(中国)特别针对中国市场优化的ODATA插件来增强其国际化和本 ...
- MVC4.0网站发布和部署到IIS7.0上的方法【转:http://www.th7.cn/Program/net/201403/183756.shtml】
最近在研究MVC4,使用vs2010,开发的站点在发布和部署到iis7上的过程中遇到了很多问题,现在将解决的过程记录下来,以便日后参考,整个过程主要以截图形式呈现 vs2010的安装和mvc4的安装不 ...
- JavaScript 字符串处理详解【转自:http://www.cnblogs.com/mondLei/p/4096855.html】
一.创建字符串 创建一个字符串,将一组字符串用引号包起来,将其赋值给一个字符串变量. var JsStr="Hello,JavaScript String!"; 二.字 ...
- MongoDB的交互(mongodb/node-mongodb-native)、MongoDB入门
MongoDB 开源,高性能的NoSQL数据库:支持索引.集群.复制和故障转移.各种语言的驱动程序:高伸缩性: NoSQL毕竟还处于发展阶段,也有说它的各种问题的:http://coolshell.c ...
- 4.FPGA芯片管脚解释
用户I/O:不用解释了. 配置管脚: MSEL[1:0] 用于选择配置模式,比如AS.PS等. DATA0 FPGA串行数据输入,连接到配置器件的串行数据输出管脚. DCLK FPGA串行时钟输出 ...
- JAVA类与对象(五)----对象的生成、使用
对象的生成 创建一个对象包括对象的声明.实例化.初始化三部分. 1.声明-----类名对象名 声明并不是为对象分配内存空间,而只是分配一个引用空间.对象的引用类似于指针,是32位的地址空间,它的值指向 ...
- ED/EP简介
ED:electronic Deposit,电子存折 EP:electronic Purse,电子钱包 PIN:personal identification number,个人识别码 MAC:Mes ...
- Asp.Net MVC3(三)-MvcApp实现全局异常捕获
定义异常捕获类: [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMu ...