ZOJ 1456 Minimum Transport Cost(Floyd算法求解最短路径并输出最小字典序路径)
题目链接:
https://vjudge.net/problem/ZOJ-1456
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
/*
题意描述
给出路径花费矩阵,路途中经过结点的过路费,问从a到b的短路径是多少,并输出路径,如果最小花费相同,输出路径字典序最小的那条
解题思路
使用Floyd,每次检测到某个节点能够松弛某条路径,就更新直接后继,二维数组记录路径的方法。
*/
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std; const int inf = ;
const int maxn = ;
int e[maxn][maxn], suc[maxn][maxn], b[maxn];
int n;
void Floyd(); int main()
{
while(scanf("%d", &n) == && n) {
for(int i = ; i <= n; i++) {
for(int j = ; j <= n; j++) {
scanf("%d", &e[i][j]);
if(e[i][j] == -)
e[i][j] = inf;
suc[i][j] = j;//从i到j的直接后继初始化为j
}
}
for(int i = ; i <= n; i++) {
scanf("%d", &b[i]);
}
Floyd();
int u, v;
while(scanf("%d%d", &u, &v) == && u + v != -) {
printf("From %d to %d :\nPath: %d", u, v, u);
int tp = u;
while(tp != v) {//一直输出u到v的直接后继,直到后继就是v
printf("-->%d", suc[tp][v]);
tp = suc[tp][v];
}
printf("\nTotal cost : %d\n\n", e[u][v]);
}
}
return ;
} void Floyd() {
for(int k = ; k <= n; k++) {
for(int i = ; i <= n; i++) {
for(int j = ; j <= n; j++) {
if(e[i][j] > e[i][k] + e[k][j] + b[k]) {
e[i][j] = e[i][k] + e[k][j] + b[k];
suc[i][j] = suc[i][k];//如果k点能够使从i到j的路径松弛,那么就将i到j的直接后继更新为从i到k的直接后继
} else if(e[i][j] == e[i][k] + e[k][j] + b[k] && suc[i][j] > suc[i][k])
suc[i][j] = suc[i][k];
}
}
}
}
ZOJ 1456 Minimum Transport Cost(Floyd算法求解最短路径并输出最小字典序路径)的更多相关文章
- ZOJ 1456 Minimum Transport Cost(floyd+后继路径记录)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1456 题意:求最短路并且输出字典序最小的答案. 思路:如果用dijkstr ...
- 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 ...
- Minimum Transport Cost Floyd 输出最短路
These are N cities in Spring country. Between each pair of cities there may be one transportation tr ...
- HDU 1385 Minimum Transport Cost (最短路,并输出路径)
题意:给你n个城市,一些城市之间会有一些道路,有边权.并且每个城市都会有一些费用. 然后你一些起点和终点,问你从起点到终点最少需要多少路途. 除了起点和终点,最短路的图中的每个城市的费用都要加上. 思 ...
- HDU1385 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 Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- HDU 1385 Minimum Transport Cost( Floyd + 记录路径 )
链接:传送门 题意:有 n 个城市,从城市 i 到城市 j 需要话费 Aij ,当穿越城市 i 的时候还需要话费额外的 Bi ( 起点终点两个城市不算穿越 ),给出 n × n 大小的城市关系图,-1 ...
- HDU 1385 Minimum Transport Cost (Dijstra 最短路)
Minimum Transport Cost http://acm.hdu.edu.cn/showproblem.php?pid=1385 Problem Description These are ...
随机推荐
- 操作系统学习笔记(三) windows内存管理
//系统物理页面是由 (Page Frame Number Database )简称PFN数据库来进行管理,实际上是一个数组,每个物理页面都对应一个PFN项. 进程的地址空间是通过VAD(Virtua ...
- Java程序员职业生涯规划
一.规划 工作3年了,感觉自己的技术现在到了一个瓶颈,在做一些重复性的业务性的工作,没有长进,提高太慢:因此停下脚步对自己的职业生涯做了一个规划,并为之努力奋斗: 20-27岁:技术积累阶段在这 5 ...
- 关于poi导出excel三种方式HSSFWorkbook,SXSSFWorkbook,csv的总结
poi导出excel最常用的是第一种方式HSSFWorkbook,不过这种方式数据量大的话会产生内存溢出问题,SXSSFWorkbook是一种大数据量导出格式,csv是另一种excel导出的一种轻快的 ...
- Centos系统安装InfluxDB
概述安装influxDB时需要root用户或者管理员权限. 端口默认情况下,InfluxDB会使用如下的端口: * TCP8086端口是服务器监听端口,对HTTP API响应 * TCP8088端口是 ...
- Android开发 - 解决DialogFragment在全屏时View被状态栏遮住的问题
我的上一篇文章:设置DialogFragment全屏显示 可以设置对话框的内容全屏显示,但是存在在某些机型上顶部的View被状态栏遮住的问题.经过测试,发现了一种解决办法,在DialogFragmen ...
- [Swift]LaunchScreen.storyboard如何跳转到到Main.storyboard
在加载App时,首先读取[LaunchScreen.storyboard]中的内容, 在App加载到内存之后,自动读取[Main.storyboard]中的初始视图控制器, 用于替换原来的[Launc ...
- Zabbix-2--安装--LAMP/LNMP详细总结
- Linux下用ifconfig命令设置IP、掩码、网关
设置IP和掩码ifconfig eth0 192.168.1.240 netmask 255.255.255.0设置网关route add default gw 192.168.1.1 每日一言:靡不 ...
- Maven - 实例-3-自动创建Maven目录骨架
archetype插件用于创建符合maven规定的目录骨架 方式一:根据提示设置相关参数 guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/Eclips ...
- cracking the coding interview系列C#实现
原版内容转自:CTCI面试系列——谷歌面试官经典作品 | 快课网 此系列为C#实现版本 谷歌面试官经典作品(CTCI)目录 1.1 判断一个字符串中的字符是否唯一 1.2 字符串翻转 1.3 去除 ...