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 ...
随机推荐
- 教你轻松快速学会用Calibre TXT转MOBI
教你轻松快速学会TXT转为有目录的MOBI###授人以渔,lllll5500制作### 需使用软件按先后顺序如下:一.排版助手 官网http://www.gidot.net/typesetter/二. ...
- 【Selenium】【BugList4】执行pip报错:Fatal error in launcher: Unable to create process using '""D:\Program Files\Python36\python.exe"" "D:\Program Files\Python36\Scripts\pip.exe" '
环境信息: python版本:V3.6.4 安装路径:D:\Program Files\python36 环境变量PATH:D:\Program Files\Python36;D:\Program F ...
- 去掉手机端延迟300ms
手机端300ms延迟是由于在手机上可以双击可以放大缩小造成的,当初ios苹果的工程师们做了一些约定,应对 iPhone 这种小屏幕浏览桌面端站点的问题.这就是手机端300ms延迟的由来. 解决:我是用 ...
- hdmi中深度色彩像素打包
4个色彩像素包模式:24- 30- 36- 48- 不同模式下tmds时钟与与像素的比是位宽与24的比值 . 24 bit mode: TMDS clock = 1.0 x pixel clock ( ...
- winSocket编程(十)完成端口
//本篇为转贴 本系列里完成端口的代码在两年前就已经写好了,但是由于许久没有写东西了,不知该如何提笔,所以这篇文档总是在酝酿之中……酝酿了两年之后,终于决定开始动笔了,但愿还不算晚….. 这篇文档我非 ...
- JavaScript ~~ECMAScript
一.JavaScript 简介 HTML:从语义的角度,描述页面结构 CSS:从审美的角度,描述样式(美化页面) JavaScript:从交互的角度,描述行为(提升用户体验) 2.JavaScript ...
- SDWebImage之UIView+WebCache
UIView+WebCache是我们能很方便的使用sd_setImageWithURL:系列方法来加载图片的关键类.UIButton(WebCache).MKAnnotationView(WebCac ...
- python 使用进程池Pool进行并发编程
进程池Pool 当需要创建的子进程数量不多时,可以直接利用multiprocessing中的Process动态成生多个进程,但如果是上百甚至上千个目标,手动的去创建进程的工作量巨大,此时就可以用到mu ...
- 利用phpcms后台漏洞渗透某色情网站
本文来源于i春秋学院,未经允许严禁转载 phpcms v9版本最近爆了好几个漏洞,网上公开了不少信息,但没有真正实战过,就不能掌握其利用方法,本次是在偶然的机会下,发现一个网站推荐楼凤信息,通过分析, ...
- 开源性能测试工具Locust使用篇(三)
脚本增强 面对较复杂的测试场景,我们可能还是会感觉无从下手:例如,很多时候脚本需要做关联或参数化处理,Locust中就不知道怎么实现了.可能也是这方面的原因,感觉难以将Locust应用到实际的性能测试 ...