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 ...
随机推荐
- python_对字符串操作.join() 效率 比 + 效率高
将列表中的字符拼接成字符串时,有两种方式 方式1:使用join()方法,将列表转为字符串 方式2:使用+运算符,循环遍历 import time str1 = ['a','b','c','d','e' ...
- uva1610 Party Games
细节值得注意 注意vector<string>是可以直接sort的! #include <iostream> #include <string> #include ...
- caffe layer注册机制
Caffe内部维护一个注册表用于查找特定Layer对应的工厂函数(Layer Factory的设计用到了设计模式里的工厂模式).Layer_factory的主要作用是负责Layer的注册,已经注册完事 ...
- poj2065 SETI
题目描述: 多组数据,每次给出一个模数$p$和一个表示答案的字符串, 相当于给出一个方程组:$$a_1*1^{1}+a_2*1^{2}+……+a_n*1^{n}=f_1$$ $$a_1*2^{1}+a ...
- Mysql when case 批量更新
UPDATE categories SET display_order = CASE id WHEN 1 THEN 3 WHEN 2 THEN 4 WHEN 3 THEN 5 END WHERE id ...
- 【linux 06】 linux中的用户权限、文件权限与目录权限
1.用户及用户组的概念: 1.文件所有者 2.用户组 3.用户 以root登录Linux之后,执行ls -al,会看到有关文件属性的信息 -rw-r--r--,第1个字符代表这个文件是“目录,文件或链 ...
- python 06 8/28-8/30
六 函数的返回值,使用return返回数据,可以同时返回多个数据,将会以元组的形式返回到函数的调用处.return 具有返回数据和中止程序的作用! return 后不加任何数据则返回None ,判定为 ...
- 第十三章:MFC库与Windows程序开发概述
主要内容: 1.Windows程序的基本结构 2.MFC库简介 3.使用Visual C++开发Windows程序 具体内容略
- python-with管理文件上下文(基本文件操作)
什么是文件 文件是操作系统为用户提供的一个读写硬盘的虚拟单位,文件的操作就是文件的读.写. 操作过程:当我们双击文件 -<- 操作系统接收到指令请求(将用户或应用程序的读写操作转换成集体的硬盘指 ...
- Centos6.5安装Nexus及安装时的一些错误
注意:此篇博文未有配置部分,有需求的同学只能自行寻找了-- 1.下载: https://www.sonatype.com/download-oss-sonatype 2.官方推荐安装在/opt目录下 ...