Minimum Transport Cost
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12860    Accepted Submission(s): 3633
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

C/C++:

 #include <map>
#include <queue>
#include <cmath>
#include <string>
#include <cstring>
#include <cstdio>
#include <climits>
#include <algorithm>
#define INF 0xffffff
using namespace std; int n, my_map[][], my_tax[], my_begin, my_end, my_path[][]; void my_floyd()
{
for (int i = ; i <= n; ++ i)
for (int j = ; j <= n; ++ j)
my_path[i][j] = j;
for (int k = ; k <= n; ++ k)
for (int i = ; i <= n; ++ i)
for (int j = ; j <= n; ++ j)
{
int my_temp = my_map[i][k] + my_map[k][j] + my_tax[k];
if (my_temp < my_map[i][j])
{
my_map[i][j] = my_temp;
my_path[i][j] = my_path[i][k];
}
else if (my_temp == my_map[i][j] && my_path[i][j] > my_path[i][k])
my_path[i][j] = my_path[i][k];
}
} int main()
{
while (~scanf("%d", &n), n)
{
for (int i = ; i <= n; ++ i)
for (int j = ; j <= n; ++ j)
{
int my_temp;
scanf("%d", &my_temp);
if (my_temp == -)
my_map[i][j] = INF;
else
my_map[i][j] = my_temp;
}
for (int i = ; i <= n; ++ i)
scanf("%d", &my_tax[i]);
my_floyd();
while (scanf("%d%d", &my_begin, &my_end), my_begin != - || my_end != -)
{
printf("From %d to %d :\n", my_begin, my_end);
printf("Path: %d", my_begin);
int my_now = my_begin;
while (my_now != my_end)
{
printf("-->%d", my_path[my_now][my_end]);
my_now = my_path[my_now][my_end];
}
printf("\nTotal cost : %d\n\n", my_map[my_begin][my_end]);
}
}
return ;
}

hdu 1385 Minimum Transport Cost (Floyd)的更多相关文章

  1. 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 ...

  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算法)

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

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

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

  5. hdu 1385 Minimum Transport Cost

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

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

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

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

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

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

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

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

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

随机推荐

  1. PMBOK(第六版) PMP笔记——《十》第十章(项目沟通管理)

    PMBOK(第六版) PMP笔记——<十>第十章(项目沟通管理) 第十章 项目沟通管理: PM 大多数时间都用在与干系人的沟通上. 第十章有三个过程: 规划沟通管理:根据干系人的需求,制定 ...

  2. Redis集群与高可用性技术小结

    客户端分片,这种方式需要实现特定的客户端,需要手工配置redis实例并根据算法进行访问,对于redis实例的增减,调整灵活性很差,一般不推荐. 代理分片,常见的有Twemproxy架构(豆瓣创建了co ...

  3. 使用python进行运动轨迹合并:多次骑行跑步轨迹叠加显示

    现有各种各样的运动app.运动手表手环以及gps码表等可以用于记录日常骑行或跑步等运动轨迹;但轨迹显示多数只限于显示一天的轨迹,经过搜索只发现一篇文章介绍跑步轨迹叠加方法(查看),根据教程尝试了下还因 ...

  4. jmeter-操作mysql

    1.下载mysql驱动并放至如下目录:E:\soft\apache-jmeter-5.1.1\lib\ext 2.添加JDBC Connection Configuration(线程组-配置元件-JD ...

  5. ESP8266开发之旅 基础篇⑤ ESP8266 SPI通信和I2C通信

        设备与设备之间的通信往往都伴随着总线的使用,而用得比较多的就当属于SPI总线和I2C总线,而恰巧NodeMcu也支持这两种总线通信,所以本章的主要内容就是讲解ESP8266 SPI和I2C总线 ...

  6. 关于M23内核简介 - 待续

    1.定位 M23是基于最新的ARMv8-M构架的主要关注低功耗应用的微控制器,未来会是M0.M0+的替代品. M33是基于最新的ARMv8-M构架的主要关注高能效应用的微控制器,未来会替换M3.M4. ...

  7. 深度讲解Linux内存管理和Linux进程调度-打通任督二脉

    我在多年的工程生涯中发现很多工程师碰到一个共性的问题:Linux工程师很多,甚至有很多有多年工作经验,但是对一些关键概念的理解非常模糊,比如不理解CPU.内存资源等的真正分布,具体的工作机制,这使得他 ...

  8. cxf 调用 .net webservice

    1.   问题背景          现在我们两套语言并行,其中必然会涉及到不同系统的相互访问. .net 的会员信息是用 webservice  提供服务的.那如何对现有 .net webservi ...

  9. The usage of Markdown---链接的使用

    目录 1. 序言 2. 网页链接 3. 图片链接 4. 页内跳转 更新时间:2019.09.14 1. 序言   在编辑文章的时候,我们常常需要插入各种链接,比如说网页链接,图片链接等等.当文章篇幅过 ...

  10. InitializingBean,spring 初始化bean

    springframework的提供接口,InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的 ...