hdu 1385 Minimum Transport Cost(floyd && 记录路径)
Minimum Transport Cost
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8794 Accepted Submission(s): 2311
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.
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
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
Asia 1996, Shanghai (Mainland China)
#include<stdio.h>
#include<string.h>
#define M 500
#define inf 0x3f3f3f3f
int dis[M][M],b[M],n,re[M][M];//re记录一条边的后驱
void floyd(){
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
if(dis[i][j] > dis[i][k]+dis[k][j]+b[k]){//注意加上这个城市的值
dis[i][j] = dis[i][k]+dis[k][j]+b[k];
re[i][j] = re[i][k];
}else{
if(dis[i][j] == dis[i][k]+dis[k][j]+b[k] && re[i][j]>re[i][k])//当路径相等的时候按字典序选择)
re[i][j] = re[i][k];
}
}
}
int main(){
int i,j,x,y;
while(~scanf("%d",&n),n){
memset(re,0,sizeof(re));
for(i=1;i<=n;i++)
for(j=1;j<=n;j++){
scanf("%d",&dis[i][j]);
if(dis[i][j]==-1)
dis[i][j]=inf;
re[i][j]=j;
}
for(i=1;i<=n;i++)
scanf("%d",&b[i]);
floyd();
while(scanf("%d%d",&x,&y),(x!=-1||y!=-1)){
printf("From %d to %d :\nPath: %d",x,y,x);
int u=x,v=y;
while(u!=v){
printf("-->%d",re[u][v]);
u=re[u][v];
}
printf("\nTotal cost : %d\n\n", dis[x][y]);
}
}
return 0;
}
hdu 1385 Minimum Transport Cost(floyd && 记录路径)的更多相关文章
- 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 (Dijstra 最短路)
Minimum Transport Cost http://acm.hdu.edu.cn/showproblem.php?pid=1385 Problem Description These are ...
- HDU 1385 Minimum Transport Cost( Floyd + 记录路径 )
链接:传送门 题意:有 n 个城市,从城市 i 到城市 j 需要话费 Aij ,当穿越城市 i 的时候还需要话费额外的 Bi ( 起点终点两个城市不算穿越 ),给出 n × n 大小的城市关系图,-1 ...
- hdu 1385 Minimum Transport Cost (floyd算法)
貌似···················· 这个算法深的东西还是很不熟悉!继续学习!!!! ++++++++++++++++++++++++++++ ======================== ...
- HDU 1385 Minimum Transport Cost (最短路,并输出路径)
题意:给你n个城市,一些城市之间会有一些道路,有边权.并且每个城市都会有一些费用. 然后你一些起点和终点,问你从起点到终点最少需要多少路途. 除了起点和终点,最短路的图中的每个城市的费用都要加上. 思 ...
- HDU 1385 Minimum Transport Cost (输出字典序最小路径)【最短路】
<题目链接> 题目大意:给你一张图,有n个点,每个点都有需要缴的税,两个直接相连点之间的道路也有需要花费的费用.现在进行多次询问,给定起点和终点,输出给定起点和终点之间最少花费是多少,并且 ...
- 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算法求全部路径的最短路径,并且须要保存路径,并且更进一步须要依照字典顺序输出结果. 还是有一定难度的. Floyd有一种非常巧妙的记录数据的方法,大多都是使用这种方法记录数据的. ...
- Minimum Transport Cost(floyd+二维数组记录路径)
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
随机推荐
- 安装 Zend Studio 报错:0x80070666
出现 0x80070666 报错时 查看日志文件,发现调用VC14(即:Microsoft Visual C++ 2015 Redistributable)时,出错返回0x666 先卸载原有的VC14 ...
- 手工修改注册表激活windows xp法
手工修改注册表激活windows xp法: 1.安装原版windows xp 2.打开注册表regedit 3.找到主键 Hkey_Local_Machine\Software\Microsoft\W ...
- Vickers Vane Pump Tips - Vane Pump Maintenance Note
The Vickers Vane Pump describes the matters needing attention in the maintenance of the vane p ...
- IDEA无法编译源码,IDEA查看源码出现/* compiled code */
打开Settings -> Plugins 搜索dec,选中,确定,重启,解决
- python3写冒泡排序
1.概念理解: 冒泡排序:可以简单的理解为是列表中相近的元素,两两比较,小的在前面.最多需要len()-1次排序. 2.例子:a=[11,7,4,56,35,0] 3.代码实现: 4.输出结果: 第1 ...
- selenium Select下拉框
先来认识一下下拉框,以百度的“高级设置”为例 介绍两种方法来处理下拉框:使用click事件,使用Select方法 使用click事件 上述下拉框的源代码如下: 虽然我们可以在html源文件中看到sel ...
- mysql5.7zip安装
一.下载mysql zip文件 二.解压.(我的目录A:\mysql\mysql-5.7.23-winx64) 三.配置环境变量 Path后面追加%A:\mysql\mysql-5.7.23-wi ...
- MySQL sys Schema
MySQL sys Schema 使用sys Schema的先决条件 使用sys Schema sys Schema Progress Reporting sys Schema Object Refe ...
- 自定义函数导致的sql性能问题
同事说,某某报表跑的很慢,让我调查一下 优化前:该报表整体需要跑4小时以上. sql代码如下 SELECT /*省略多数查询字段*/ REP_FUN_REFCODEVALUE /*自定义函数*/ (P ...
- (2) OpenSSL命令
openssl命令的格式是"openssl command command-options args",command部分有很多种命令,这些命令需要依赖于openssl命令才能执行 ...