题目链接:

思路:

输出路径的最短路变种问题。。这个题目在于多组询问。那么个人认为用floyd更加稳妥一点。还有就是在每一个城市都有过路费,所以在floyd的时候更改一下松弛条件就可以。。那么输出路径怎么办呢??我採用的是输出起点的后继而不是终点的前驱。。由于我们关心的是路径字典序最小,关心的是起点的后继。。。那么打印路径的时候就直接从前向后打印,这个和dijkstra的打印路径稍有不同。。。

最短路的打印參见传送门

题目:

Minimum Transport Cost

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 7538    Accepted Submission(s): 1935

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
 
Recommend


代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define INF 0x3f3f3f3f
using namespace std; const int maxn=50+10;
int dis[maxn][maxn],path[maxn][maxn],n,cost[maxn];
int u,st,en; void floyd()
{
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
int tmp=dis[i][k]+dis[k][j]+cost[k];
if(tmp<dis[i][j]||(tmp==dis[i][j]&&path[i][j]>path[i][k]))
{
dis[i][j]=tmp;
path[i][j]=path[i][k];
}
}
} void read_Graph()
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
scanf("%d",&u);
if(u==-1)
dis[i][j]=INF;
else
{
dis[i][j]=u;
path[i][j]=j;
}
}
for(int i=1;i<=n;i++)
scanf("%d",&cost[i]);
} void solve()
{
while(~scanf("%d%d",&st,&en))
{
if(st==-1&&en==-1) break;
printf("From %d to %d :\n",st,en);
printf("Path: %d",st);
int Gery=st;
while(Gery!=en)
{
printf("-->%d",path[Gery][en]);
Gery=path[Gery][en];
}
printf("\nTotal cost : %d\n\n",dis[st][en]);
}
} int main()
{
while(~scanf("%d",&n),n)
{
read_Graph();
floyd();
solve();
}
return 0;
}


hdu1385Minimum Transport Cost(最短路变种)的更多相关文章

  1. 【堆优化Dijkstra+字典序最短路方案】HDU1385-Minimum Transport Cost

    [题目大意] 给出邻接矩阵以及到达各个点需要付出的代价(起点和终点没有代价),求出从给定起点到终点的最短路,并输出字典序最小的方案. [思路] 在堆优化Dijkstra中,用pre记录前驱.如果新方案 ...

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

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

  3. Minimum Transport Cost Floyd 输出最短路

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

  4. HD1385Minimum Transport Cost(Floyd + 输出路径)

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

  5. poj 1797 Heavy Transportation(最短路变种2,连通图的最小边)

    题目 改动见下,请自行画图理解 具体细节也请看下面的代码: 这个花了300多ms #define _CRT_SECURE_NO_WARNINGS #include<string.h> #i ...

  6. poj 2253 Frogger (最短路变种,连通图的最长边)

    题目 这里的dijsktra的变种代码是我看着自己打的,终于把代码和做法思路联系上了,也就是理解了算法——看来手跟着画一遍真的有助于理解. #define _CRT_SECURE_NO_WARNING ...

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

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

  8. NSOJ Minimum Transport Cost

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

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

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

随机推荐

  1. RN踩坑

    使用夜神 使用夜神作为模拟器,这个模拟器启动就会监听62001端口. 开发工具与模拟器的通信都是通过adb.夜神模拟器的安装目录/bin下有一个adb.exe,android sdk tools下也有 ...

  2. python 游戏(滑动拼图Slide_Puzzle)

    1. 游戏功能和流程图 实现16宫格滑动拼图,实现3个按钮(重置用户操作,重新开始游戏,解密游戏),后续难度,额外添加重置一次的按钮,解密算法的植入,数字改变为图片植入 游戏流程图 2. 游戏配置 配 ...

  3. 03005_Tomcat

    1.Tomcat下载 (1)Tomcat解压版:链接:Tomcat解压版 密码:0iw0 : (2)源码:链接:源码 密码:3o43 . 2.Tomcat的目录结构 (1)bin:脚本目录   ①启动 ...

  4. Knockout v3.4.0 中文版教程-2-监控-通过监控创建视图模型(上)

    2. 监控 1.通过监控创建视图模型 1. 监控 Knockout是基于以下三个核心特性: 监控和依赖跟踪 声明式绑定 模板 在本节,你将第一次了解这三个特性,在这之前,我们先来了解以下MVVM模式和 ...

  5. luogu2483 【模板】k短路([SDOI2010]魔法猪学院)

    模板题 #include <iostream> #include <cstring> #include <cstdio> #include <queue> ...

  6. 【LeetCode】Linked List Cycle(环形链表)

    这道题是LeetCode里的第141道题. 题目要求: 给定一个链表,判断链表中是否有环. 进阶: 你能否不使用额外空间解决此题? 简单题,但是还是得学一下这道题的做法,这道题是用双指针一个fast, ...

  7. NYOJ 311 完全背包

    完全背包 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 直接说题意,完全背包定义有N种物品和一个容量为V的背包,每种物品都有无限件可用.第i种物品的体积是c,价值是 ...

  8. 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛

    02Train Seats Reservation 问答 只看题面 33.87% 1000ms 131072K You are given a list of train stations, say ...

  9. .NET重构(六):删除用户和结账的理解

    导读:这是第二回机房了,第一回不明不白,不清不楚的就过去了(相对),这一回,有了新的发现.就是在用户删除的时候,涉及到的一些逻辑问题,以及结账时的数据来源问题. 一.用户删除 问题:第一次机房,包括重 ...

  10. 洛谷 [P2734] 游戏

    博弈论+区间dp 有博弈论吗?大约只有一个博弈论的壳子 设 dp[i][j] 表示区间 i ~ j 先手最多能取多少, 它可以由 i ~ j - 1 与 i + 1 ~ j 来转移, 等于上述两个区间 ...