Minimum Transport Cost Floyd 输出最短路
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.
InputFirst 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:
OutputFrom 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
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 103
#define L 31
#define INF 1000000009
#define eps 0.00000001
int g[MAXN][MAXN],path[MAXN][MAXN], n, v[MAXN];//path[i][j] 表示路径i->j上 i之后的第一个结点
void Floyd()
{
for (int k = ; k <= n; k++)
{
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n; j++)
{
if (g[i][j] > g[i][k] + g[k][j] + v[k])
{
g[i][j] = g[i][k] + g[k][j] + v[k];
path[i][j] = path[i][k];//将路径结点缩小范围确定
}
else if (g[i][j] == g[i][k] + g[k][j] + v[k])
{
if (path[i][j] > path[i][k])
{
path[i][j] = path[i][k];//比较前面的字典序
}
}
}
}
}
}
void Print(int u, int v)//递归 从i->j 可以分解为 i->path[i][j]->path[path[i][j]][j]->,,,,,j
{
if (u == v)
{
printf("%d\n", u);
return;
}
int k = path[u][v];
printf("%d-->", u);
Print(k, v);
}
int main()
{
while (scanf("%d", &n), n)
{
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n; j++)
{
scanf("%d", &g[i][j]);
path[i][j] = j;
if (g[i][j] == -) g[i][j] = INF;
}
}
for (int i = ; i <= n; i++)
scanf("%d", &v[i]);
int f, t;
Floyd();
while (scanf("%d%d", &f, &t), f != - && t != -)
{
printf("From %d to %d :\nPath: ", f, t);
Print(f, t);
printf("Total cost : %d\n\n", g[f][t]);
}
}
}
Minimum Transport Cost Floyd 输出最短路的更多相关文章
- 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 (Dijstra 最短路)
Minimum Transport Cost http://acm.hdu.edu.cn/showproblem.php?pid=1385 Problem Description These are ...
- 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 ...
- ZOJ 1456 Minimum Transport Cost(Floyd算法求解最短路径并输出最小字典序路径)
题目链接: https://vjudge.net/problem/ZOJ-1456 These are N cities in Spring country. Between each pair of ...
- HDU 1385 Minimum Transport Cost (输出字典序最小路径)【最短路】
<题目链接> 题目大意:给你一张图,有n个点,每个点都有需要缴的税,两个直接相连点之间的道路也有需要花费的费用.现在进行多次询问,给定起点和终点,输出给定起点和终点之间最少花费是多少,并且 ...
- NSOJ Minimum Transport Cost
These are N cities in Spring country. Between each pair of cities there may be one transportation tr ...
- HDU 1385 Minimum Transport Cost (最短路,并输出路径)
题意:给你n个城市,一些城市之间会有一些道路,有边权.并且每个城市都会有一些费用. 然后你一些起点和终点,问你从起点到终点最少需要多少路途. 除了起点和终点,最短路的图中的每个城市的费用都要加上. 思 ...
随机推荐
- 整理 Xamarin.Forms - Plugins
Open Source Components for Xamarin Xamarin官方整理的一些开源组件,有需要可以先到这里找 GitHub: xamarin/XamarinComponents: ...
- linux rpm 安装
1.rpm 安装rpm -ivh package_name-i:install的意思-v:查看更详细的安装信息-h:以安装信息栏显示安装进度rpm -ivh package_name --test 2 ...
- 构造 Codeforces Round #275 (Div. 2) C. Diverse Permutation
题目传送门 /* 构造:首先先选好k个不同的值,从1到k,按要求把数字放好,其余的随便放.因为是绝对差值,从n开始一下一上, 这样保证不会超出边界并且以防其余的数相邻绝对值差>k */ /*** ...
- JavaScript01天学习笔记分享
01知识点 JavaScript 代码运行在浏览器(后缀名.js) 和java完全不同的东西,只是名称类型而已 src 引用脚本 <Script></Script> ale ...
- web开发----jsp中通用模版的引用 include的用法
1.静态引入的示例 通过对两种用法的了解之后 我们现在 使用静态引入 因为上述原因 我的模版页中 只有div 不会有 path等定义 也不会有html标签 如下: 我的header.jsp 全 ...
- System.AppDomain类详解(二)
进程是存在独立的内存和资源的,但是AppDomain仅仅是逻辑上的一种抽象.一个process可以存在多个AppDomain.各个AppDomain之间的数据时相互独立的.一个线程可以穿梭多个AppD ...
- dubbo与zookeeper学习中的问题
环境: spring5.1.5 dubbo 2.6.2 异常信息: java.lang.NoClassDefFoundError: org/apache/curator/RetryPolicy at ...
- 使用libpqxx访问PostgreSQL数据库(mingw编译libpqxx)
编译前准备 1. 安装mingw 安装mingw(不管是直接安装mingw还是其他如code::blocks附带安装的mingw),输入:gcc -v可显示如下图的版本信息,我的版本是mingw ...
- ubuntu16.04安装teamviewer12
安装teamviewer下载地址:http://www.teamviewer.com/en/download/linux/ 下载的是:teamviewer_12.0.76279_i386.deb ...
- How a stack frame works 栈帧的要素与构建步骤
http://en.citizendium.org/wiki/Stack_frame To use a stack frame, a thread keeps two pointers, often ...