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 ...
随机推荐
- python入门总结-函数
函数形式: def functionname(paramlist): function body 局部变量不改变实参的值,如果需要改变,声明global.比如,global x 可以给函数默认值,注意 ...
- linux下更改文件夹所属用户和用户组
改变所属用户组:chgrp -R users filename -R是为了递归改变文件夹下的文件和文件夹,users是要改为的用户组名称,filename是要改变的文件夹名称 ============ ...
- hdu 5199 Gunner
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=5199 简单题,stl水之... #include<algorithm> #include& ...
- swift学习初步(三)--控制流操作
在上一篇博客里面,我谈到了swift里面的一些基本类型以及相关的操作,相信你看了之后一定会觉得其实swift也不难嘛.好吧,这篇博客里面要谈的一些高级操作,可能会让你有点头疼了. 好了,废话不多说了, ...
- Openstack:ice-house安装过程
#apt-get install ntpdpkg-reconfigure tzdata --> Asia -->Shuanghai #apt-get install python-mysq ...
- 【转载】FPGA静态时序分析——IO口时序
转自:http://www.cnblogs.com/linjie-swust/archive/2012/03/01/FPGA.html 1.1 概述 在高速系统中FPGA时序约束不止包括内部时钟约束 ...
- VC++编程之对话框贴图
基于对话框的程序写好后,为对话框贴上个图片让界面更加美观(我承认做界面,MFC显得力不从心,不如QT). 其实很简单,我们以位图为例,选好我们需要的位图资源(bmp),假若自己的图片不是位图资源,可以 ...
- SQL Server Reporting Services – Insufficient Rights Error
http://www.sql-server-performance.com/2011/security-ssrs-reporting-error/ SQL Server Reporting Servi ...
- 四则运算三+psp0级表格
一.题目 在四则运算二的基础上,选择一个方向进行拓展,我选择的是增加了答题模块 二.设计思路 1.在上次的基础上,增加了答题模块,每出现一道四则运算题目,便提醒输入结果,如果结果错误,就会提示错误 2 ...
- BCP command usage in SQL Server
The bcp Command-Line Utility You use the bcp (bulk copy program) tool to address the bulk movement o ...