Minimum Transport Cost

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8794    Accepted Submission(s): 2311

Problem Description
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

 

Source

Asia 1996, Shanghai (Mainland China) 

#include<stdio.h>
#include<string.h>
#define M 500
#define inf 0x3f3f3f3f
int dis[M][M],b[M],n,re[M][M];//re记录一条边的后驱
void floyd(){
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
if(dis[i][j] > dis[i][k]+dis[k][j]+b[k]){//注意加上这个城市的值
dis[i][j] = dis[i][k]+dis[k][j]+b[k];
re[i][j] = re[i][k];
}else{
if(dis[i][j] == dis[i][k]+dis[k][j]+b[k] && re[i][j]>re[i][k])//当路径相等的时候按字典序选择)
re[i][j] = re[i][k];
}
}
}
int main(){
int i,j,x,y;
while(~scanf("%d",&n),n){
memset(re,0,sizeof(re));
for(i=1;i<=n;i++)
for(j=1;j<=n;j++){
scanf("%d",&dis[i][j]);
if(dis[i][j]==-1)
dis[i][j]=inf;
re[i][j]=j;
}
for(i=1;i<=n;i++)
scanf("%d",&b[i]);
floyd();
while(scanf("%d%d",&x,&y),(x!=-1||y!=-1)){
printf("From %d to %d :\nPath: %d",x,y,x);
int u=x,v=y;
while(u!=v){
printf("-->%d",re[u][v]);
u=re[u][v];
}
printf("\nTotal cost : %d\n\n", dis[x][y]);
}
}
return 0;
}

hdu 1385 Minimum Transport Cost(floyd &amp;&amp; 记录路径)的更多相关文章

  1. hdu 1385 Minimum Transport Cost (Floyd)

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

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

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

  3. HDU 1385 Minimum Transport Cost( Floyd + 记录路径 )

    链接:传送门 题意:有 n 个城市,从城市 i 到城市 j 需要话费 Aij ,当穿越城市 i 的时候还需要话费额外的 Bi ( 起点终点两个城市不算穿越 ),给出 n × n 大小的城市关系图,-1 ...

  4. hdu 1385 Minimum Transport Cost (floyd算法)

    貌似···················· 这个算法深的东西还是很不熟悉!继续学习!!!! ++++++++++++++++++++++++++++ ======================== ...

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

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

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

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

  7. hdu 1385 Minimum Transport Cost

    http://acm.hdu.edu.cn/showproblem.php?pid=1385 #include <cstdio> #include <cstring> #inc ...

  8. HDU 1385 Minimum Transport Cost 最短路径题解

    本题就是使用Floyd算法求全部路径的最短路径,并且须要保存路径,并且更进一步须要依照字典顺序输出结果. 还是有一定难度的. Floyd有一种非常巧妙的记录数据的方法,大多都是使用这种方法记录数据的. ...

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

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

随机推荐

  1. Python虚拟环境 之 virtualenv 与 virtualenvwrapper

           在开发Python应用程序的时候,比如系统安装的Python3只有一个版本:3.6.所有第三方的包都会被 pip 安装到Python3的 site-packages 目录下. 如果我们要 ...

  2. IIS实现HTTPS的主机名绑定

    默认情况下,IIS中HTTPS 绑定是无法指定主机名的解决办法:通过手工修改 IIS 配置来实现主机头绑定.打开如下位置的文件. C:\Windows\system32\inetsrv\config\ ...

  3. java 垃圾回收之标记算法

    对象被判定为垃圾的标准 1.没有被其他对象引用 判定对象是否为垃圾的算法 1.引用计数算法(不是主流垃圾回收机制) 1.1 判定对象的引用数量 1.1.1 通过判断对象的引用数量来决定对象是否可以被回 ...

  4. Linux 系统内存分析

    1. 内存基本介绍 1.计算机基本结构: 电脑之父--冯·诺伊曼提出了计算机的五大部件:输入设备.输出设备.存储器.运算器和控制器 如图: 输入设备:键盘鼠标等 CPU:是计算机的运算核心和控制核心, ...

  5. 任务备忘(已经完成):用python写一个格式化xml字符串的程序

    功能: 1.将xml中多余的空格,换行符去掉,让xml字符串变成一行. 2.将xml中添加缩进,使用print能正确打印添加缩进后的字符串. 思路: 采用正则表达式来判断xml中字符串的类型: 1.文 ...

  6. 深入Linux内核架构——锁与进程间通信

    Linux作为多任务系统,当一个进程生成的数据传输到另一个进程时,或数据由多个进程共享时,或进程必须彼此等待时,或需要协调资源的使用时,应用程序必须彼此通信. 一.控制机制 1.竞态条件 几个进程在访 ...

  7. JavaScript中函数的调用

    JavaScript中函数的调用 制作人:全心全意 在JavaScript中,函数定义后并不会自动执行,要执行一个函数需要在特定的位置调用该函数,调用函数需要创建调用语句,调用语句包含函数名称和参数. ...

  8. 关于自由拖拽完成的剪切区域(UI组件之图片剪切器)

    var x, y,areaWidth,areaHeight; var down;//闪光的判断标准,很好 addEvent(canvas,'mousedown',function(e){ // con ...

  9. 关于构造函数什么值传递给他的实例,只有this和prototype

    var a= function (){var bb = 12; this.aa ="xxx"}; a.aa="www"; a.prototype.cc=&quo ...

  10. Fidder详解-工具简介(保存会话、decode解码、Repaly、自定义会话框、隐藏会话、会话排序)

    前言 本文会对Fidder这款工具的一些重要功能,进行详细讲解,带大家进入Fidder的世界,本文会让你明白,Fidder不仅是一个抓包分析工具,也是一个请求发送工具,更加可以当作为Mock Serv ...