题目链接:

思路:

输出路径的最短路变种问题。。这个题目在于多组询问。那么个人认为用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. tkinter学习-文本框

    阅读目录 Entry 输入框 Text 文本框 Entry: 说明:输入控件,用于显示简单的文本内容 属性:在输入框中用代码添加和删除内容,同样也是用insert()和delete()方法 from ...

  2. usb3.0驱动

    usb3.0驱动下载地址 华硕注入usb3.0驱动工具下载地址 https://dlsvr04.asus.com/pub/ASUS/misc/utils/ASUS_EZInstaller_V10306 ...

  3. DELL R730 服务器拷贝大文件

    从服务器上拷贝大文件,通过USB拷贝,写入速度很慢,而且拷贝到100多G的时候直接卡死. 原因:服务器的USB是2.0,传输速率很慢. 解决方法: 找一台笔记本,USB 接口是3.0的,通过网络共享传 ...

  4. Windows环境下python3.7版本怎么安装pygame

    访问此网址 下载对应Python版本的pygame,如下图: 下载完成后,会有一个whl后缀的文件. 将此文件复制到Python根目录下的scripts目录下,打开cmd, 切换到scripts目录下 ...

  5. 数据结构( Pyhon 语言描述 ) — — 第7章:栈

    栈概览 栈是线性集合,遵从后进先出原则( Last - in first - out , LIFO )原则 栈常用的操作包括压入( push ) 和弹出( pop ) 栈的应用 将中缀表达式转换为后缀 ...

  6. Python语言程序设计之三--列表List常见操作和错误总结

    最近在学习列表,在这里卡住了很久,主要是课后习题太多,而且难度也不小.像我看的这本<Python语言程序设计>--梁勇著,列表和多维列表两章课后习题就有93道之多.我的天!但是题目出的非常 ...

  7. HDU 3480 DP 斜率优化 Division

    把n个数分成m段,每段的值为(MAX - MIN)2,求所能划分得到的最小值. 依然是先从小到大排个序,定义状态d(j, i)表示把前i个数划分成j段,所得到的最小值,则有状态转移方程: d(j, i ...

  8. css 标题

    纯CSS制作的复古风格的大标题 .vintage{ background: #EEE url(data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAA ...

  9. 00036_private

    1.私有private 描述人.Person: 属性:年龄: 行为:说话:说出自己的年龄. class Person { int age; String name; public void show( ...

  10. Python开发:模块

    在前面的几个章节中我们脚本上是用 python 解释器来编程,如果你从 Python 解释器退出再进入,那么你定义的所有的方法和变量就都消失了. 为此 Python 提供了一个办法,把这些定义存放在文 ...