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.

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 输出最短路的更多相关文章

  1. Minimum Transport Cost(floyd+二维数组记录路径)

    Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  2. hdu 1385 Minimum Transport Cost (Floyd)

    Minimum Transport CostTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  3. HDU 1385 Minimum Transport Cost (Dijstra 最短路)

    Minimum Transport Cost http://acm.hdu.edu.cn/showproblem.php?pid=1385 Problem Description These are ...

  4. HDU1385 Minimum Transport Cost (Floyd)

    Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  5. hdu 1385 Minimum Transport Cost(floyd &amp;&amp; 记录路径)

    Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  6. ZOJ 1456 Minimum Transport Cost(Floyd算法求解最短路径并输出最小字典序路径)

    题目链接: https://vjudge.net/problem/ZOJ-1456 These are N cities in Spring country. Between each pair of ...

  7. HDU 1385 Minimum Transport Cost (输出字典序最小路径)【最短路】

    <题目链接> 题目大意:给你一张图,有n个点,每个点都有需要缴的税,两个直接相连点之间的道路也有需要花费的费用.现在进行多次询问,给定起点和终点,输出给定起点和终点之间最少花费是多少,并且 ...

  8. NSOJ Minimum Transport Cost

    These are N cities in Spring country. Between each pair of cities there may be one transportation tr ...

  9. HDU 1385 Minimum Transport Cost (最短路,并输出路径)

    题意:给你n个城市,一些城市之间会有一些道路,有边权.并且每个城市都会有一些费用. 然后你一些起点和终点,问你从起点到终点最少需要多少路途. 除了起点和终点,最短路的图中的每个城市的费用都要加上. 思 ...

随机推荐

  1. Java多线程(一) Thread和 Runnable

    http://www.cnblogs.com/lwbqqyumidi/p/3804883.html 1.继承Thread 2.实现Runnable接口 public class MyRunnable ...

  2. CF17C Balance

    题意 [题目描述] 一个仅由a,b,c三种字符组成的字符串,可以对其进行如下两种操作: 选择两个相邻字符,将第一个字符替换成第二个. 选择两个相邻字符,将第二个字符替换成第一个. 这样,通过任意多次的 ...

  3. [Usaco2018 Open]Talent Show

    Description FarmerJohn要带着他的N头奶牛,方便起见编号为1-N,到农业展览会上去,参加每年的达牛秀!他的第i头奶牛重量为wi,才艺水平为ti,两者都是整数.在到达时,Farmer ...

  4. ACM_校庆素数

    校庆素数 Time Limit: 2000/1000ms (Java/Others) Problem Description: 广财建校33年了,如今迎来了她的校庆. 小财最近想在研究素数,她突发奇想 ...

  5. P3373 【模板】线段树 2 区间求和 区间乘 区间加

    题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.将某区间每一个数乘上x 3.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含三个整数N.M.P,分别 ...

  6. moment.js 两个时间段的截取

    var a = moment([2008, 9, 29]);var b = moment([2007, 0, 10]);console.log(a.diff(b,'months'));//‘month ...

  7. Linux学习日记之crontab使用notify-send实现每小时通知提醒

    crontab命令用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和执行 通过crontab -e 可以打开编辑文件添加新的命令 notif ...

  8. Linux学习笔记之Linux命令

    1. blkid  查看当前系统中所有已挂载文件系统的类型

  9. Sublime——基本操作

    基本安装 程序下载地址:https://www.sublimetext.com/ package control安装 View -> Show Console打开控制台或者用快捷键ctrl+~打 ...

  10. 关于.Net的强名称(Strong Name)

    下面是我在CSDN上发表的<关于.Net的强名称(Strong Name)>,转载于此. 关于.Net的强名称(Strong Name)