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个城市,一些城市之间会有一些道路,有边权.并且每个城市都会有一些费用. 然后你一些起点和终点,问你从起点到终点最少需要多少路途. 除了起点和终点,最短路的图中的每个城市的费用都要加上. 思 ...
随机推荐
- SpringMVC实现Action的两种方式以及与Struts2的区别
4.程序员写的Action可采用哪两种方式? 第一.实现Controller接口第二.继承自AbstractCommandController接口 5.springmvc与struts2的区别? 第一 ...
- oracle 自定义类型 type / create type
一:Oracle中的类型有很多种,主要可以分为以下几类: 1.字符串类型.如:char.nchar.varchar2.nvarchar2. 2.数值类型.如:int.number(p,s).integ ...
- Windows Azure中文博客 Windows Azure入门教学系列 (一): 创建第一个WebRole程序
http://blogs.msdn.com/b/azchina/ 本文转自:http://blogs.msdn.com/b/azchina/archive/2010/02/09/windows-azu ...
- micropython陀螺仪控制舵机
2018-03-1220:14:00 import pyb import time from pyb import Pin xlights = (pyb.LED(2),pyb.LED(3)) MO = ...
- android视频播放器系列(二)——VideoView
最近在学习视频相关的知识,现在也是在按部就班的一步步的来,如果有同样需求的同学可以跟着大家一起促进学习. 上一节说到了可以使用系统播放器以及浏览器播放本地以及网络视频,但是这在很大程度上并不能满足我们 ...
- 实例化Class类的5种方式
Java的数据类型可以分为两类,即引用类型和原始类型.对于每种类型的对象,Java虚拟机会实例化不可变的java.lang. Class对象.它提供了在运行时检查对象属性的方法,这些属性包括它的成员和 ...
- UVM基础之------uvm_port_base
Port Base Classes uvm_port_component_base This class defines an interface for obtaining a port ...
- swing jTable排序问题(点击表头排序)
1.JDK6自带排序实现: tableName.setAutoCreateRowSorter(true); 2.其实界面设计中勾选一个属性就搞定了: .
- parsley.js验证的基本引用
前段时间看到博客有些parsley.js验证,只是对parsley.js验证框架基本的应用,对parsley.js更深层理解没有介绍和demo 比如:异步请求,扩展验证的写法,我把我学到的parsle ...
- MySQL单表数据不超过500万:是经验数值,还是黄金铁律?
今天,探讨一个有趣的话题:MySQL 单表数据达到多少时才需要考虑分库分表?有人说 2000 万行,也有人说 500 万行.那么,你觉得这个数值多少才合适呢? 曾经在中国互联网技术圈广为流传着这么一个 ...