HD1385Minimum Transport Cost(Floyd + 输出路径)
Minimum Transport Cost
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9109 Accepted Submission(s): 2405
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.
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:
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.
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string.h>
using namespace std;
const int INF = << ;
const int MAX = ;
int n,path[MAX][MAX],dist[MAX][MAX],tax[MAX];
/path[i][j]用来保存 i --> j 的最短路径中 i 的最优后驱(即最近),在Floyd三重循环时,一直更新path。
void Floyd(int n)
{
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
path[i][j] = j;
} for(int k = ; k <= n; k++)
{
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
{
if(dist[i][k] < INF && dist[k][j] < INF)
{
int temp = dist[i][k] + dist[k][j] + tax[k];
if(temp < dist[i][j])
{
dist[i][j] = temp;
path[i][j] = path[i][k];
}
else if(temp == dist[i][j]) //因为是按照字典序输出路径因此当距离相等时,取路径中最小的那个
{
if(path[i][j] > path[i][k])
path[i][j] = path[i][k];
}
}
}
}
} }
int main()
{
while(scanf("%d", &n) != EOF && n)
{
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
{
scanf("%d", &dist[i][j]);
if(dist[i][j] == -)
dist[i][j] = INF;
}
}
for(int i = ; i <= n; i++)
scanf("%d", &tax[i]);
Floyd(n);
int start,goal;
while(scanf("%d%d", &start,&goal) != EOF)
{
if(start == - && goal == -)
break;
printf("From %d to %d :\n",start,goal); int temp = start;
printf("Path: %d",start);
while(temp != goal)
{
printf("-->%d", path[temp][goal]);
temp = path[temp][goal];
}
printf("\n");
/*
int temp = path[start][goal]; //就是这个一直把start和goal当成不会重复的了,脑子啊~
printf("Path: %d",start);
while(temp != goal)
{
printf("-->%d",temp);
temp = path[temp][goal];
}
printf("-->%d\n",goal);
*/
printf("Total cost : %d\n",dist[start][goal]);
printf("\n");
}
}
return ;
}
HD1385Minimum Transport Cost(Floyd + 输出路径)的更多相关文章
- Minimum Transport Cost Floyd 输出最短路
These are N cities in Spring country. Between each pair of cities there may be one transportation tr ...
- hdu 1385 Floyd 输出路径
Floyd 输出路径 Sample Input50 3 22 -1 43 0 5 -1 -122 5 0 9 20-1 -1 9 0 44 -1 20 4 05 17 8 3 1 //收费1 3 // ...
- 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 ...
- 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个城市,一些城市之间会有一些道路,有边权.并且每个城市都会有一些费用. 然后你一些起点和终点,问你从起点到终点最少需要多少路途. 除了起点和终点,最短路的图中的每个城市的费用都要加上. 思 ...
- hdu 1385 Minimum Transport Cost(floyd && 记录路径)
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- hdu Minimum Transport Cost(按字典序输出路径)
http://acm.hdu.edu.cn/showproblem.php? pid=1385 求最短路.要求输出字典序最小的路径. spfa:拿一个pre[]记录前驱,不同的是在松弛的时候.要考虑和 ...
- HDU1385 Minimum Transport Cost (Floyd)
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
随机推荐
- linux下正向代理/反向代理/透明代理使用说明
代理服务技术对于网站架构部署时非常重要的,一般实现代理技术的方式就是在服务器上安装代理服务软件,让其成为一个代理服务器,从而实现代理技术.常用的代理技术分为正向代理.反向代理和透明代理.以下就是针对这 ...
- kprobe原理解析(二)
上一篇文章和大家简要说明了下kprobe到底应该怎样用,那么现在我们就揭开kprobe神秘的面纱,刨根问底,一睹kprobe的庐山真面目. kprobe的工作过程大致如下: 1)注册kprobe.注册 ...
- WinForm中异步加载数据并使用进度条
在WinForm程序中,有时会因为加载大量数据导致UI界面假死,这种情况对于用户来说是非常不友好的.因此,在加载大量数据的情况下,首先应该将数据加载放在另一线程中进行,这样保证了UI界面的响应:其次可 ...
- [每日自动更新]Hillstone 山石网科 StoneOS ISP路由表配置文件
1.数据基于APNIC,准确有效 2.适用于StoneOS 4.0~5.5各版本 3.对APNIC数据进行路由聚合,实现最小子网 4.覆盖中国大陆地区电信.联通.移动三大运营商,长宽.电信通等二级运营 ...
- 微软职位内部推荐-Senior Development Engineer
微软近期Open的职位: Job Title: Senior Software Development Engineering Work Location: Suzhou, China Enterpr ...
- 客户端缓存(Client Cache)
通常在服务器端大家都已经做了很多缓存的工作,ASP.NET CACHE也好MemeryCache也好却总是忽略了客户端缓存. 因为大家都知道不管哪个client都会缓存已经访问过的站点,但是浏览器缓存 ...
- Page Security
参见开发文档 Overview This document describes how to build applications that grant selected access to indi ...
- Windows和Linux上用C与Lua交互
Windos2010编译lua的方法: http://blog.csdn.net/appletreesujie/article/details/12065369 Linux编译lua的方法: make ...
- Linux常用的基本命令
man命令:查看帮助信息 格式:man 需要查看的命令 date命令:显示时间 格式:# date ...
- Django添加Last-Modified和ETag
用Django REST Framework做的REST API,其中有个API有这样的需求: APP端请求这个API,服务器端从数据库读数据,返回json.返回的数据量稍微有些大,但是可能一年才修改 ...