题目链接:

思路:

输出路径的最短路变种问题。。这个题目在于多组询问。那么个人认为用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. 【树形dp 最长链】bzoj1912: [Apio2010]patrol 巡逻

    富有思维性的树形dp Description Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示村庄a与b之间有一条道路(1 ≤ a, ...

  2. CDC相关知识点总结

    跨时钟域设计很重要,每一个项目都会有,这个要马上学会总结好.本能力属于重要而且急需的能力.

  3. Python面向对象(类之间的关系)(三)

    类与类之间的关系 在我们的世界中事物和事物之间总会有一些联系. 在面向对象中. 类和类之间也可以产生相关的关系 1. 依赖关系 执行某个动作的时候. 需要xxx来帮助你完成这个操作. 此时的关系是最轻 ...

  4. HDU 2829 斜率优化DP Lawrence

    题意:n个数之间放m个障碍,分隔成m+1段.对于每段两两数相乘再求和,然后把这m+1个值加起来,让这个值最小. 设: d(i, j)表示前i个数之间放j个炸弹能得到的最小值 sum(i)为前缀和,co ...

  5. Linux下安装SaltStack

    一.配置yum源和epel源 epel源下载地址:http://pan.baidu.com/s/1o7NJ26u 1.配置yum源 (1)上传操作系统镜像文件来配置yum源,挂载点目录为/yum mk ...

  6. python算法-插入排序

    插入排序 一.核心思想:在一个有序的数组中,通过逐一和前面的数进行比较,找到新数的位置. 例子:数组有有一个数21 插入一个3,3<21,因此结果为 3,21 再插入一个34,34>21, ...

  7. Linux暂停和恢复进程

    Linux暂停和恢复进程 kill -STOP 1234 将该进程暂停. 如果要让它恢复到后台,用kill -CONT 1234 (很多在前台运行的程序这样是不行的) 如果要恢复到前台,请在当时运行该 ...

  8. ecmascript6入门

    ECMAScript 6 入门  阮一峰

  9. HDU——3786找出直系亲属(DFS+回溯)

    找出直系亲属 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  10. Opencv学习笔记——视频进度条的随动

    1. CvCapture结构体: CvCapture是一个结构体,用来保存图像捕获的信息,就像一种数据类型(如int,char等)只是存放的内容不一样,在OpenCv中,它最大的作用就是处理视频时(程 ...