Minimum Transport Cost

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9814    Accepted Submission(s): 2641

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
 
path数组用得太好了。。想不到啊。。floyed的理解要相当深入才行
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
const int N = ;
const int INF = ;
int graph[N][N];
int path[N][N];
int cost[N];
int main()
{
int n;
while(~scanf("%d",&n),n){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
scanf("%d",&graph[i][j]);
if(graph[i][j]==-) graph[i][j] = INF;
path[i][j]=j;
}
}
for(int i=;i<=n;i++) scanf("%d",&cost[i]);
for(int k=;k<=n;k++){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(graph[i][j]>graph[i][k]+graph[k][j]+cost[k]){
graph[i][j] = graph[i][k]+graph[k][j]+cost[k];
path[i][j]=path[i][k];
}
else if(graph[i][j]==graph[i][k]+graph[k][j]+cost[k]&&path[i][j] > path[i][k]){
path[i][j]=path[i][k];
}
}
}
}
int a,b;
while(~scanf("%d%d",&a,&b)){
if(a==-&&b==-) break;
printf("From %d to %d :\nPath: %d",a,b,a);
int u =a,v=b;
while(u!=v){
printf("-->%d",path[u][v]);
u = path[u][v];
}
printf("\nTotal cost : %d\n\n", graph[a][b]);
} }
return ;
}

hdu 1385(Floyed+打印路径好题)的更多相关文章

  1. *HDU 1385 最短路 路径

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

  2. 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 // ...

  3. hdu 1385 floyd记录路径

    可以用floyd 直接记录相应路径 太棒了! http://blog.csdn.net/ice_crazy/article/details/7785111 #include"stdio.h& ...

  4. CodeForces 10D. LCIS 最长公共上升子序列模板题 + 打印路径

    推荐一篇炒鸡赞的blog. 以下代码中有打印路径. #include <algorithm> #include <iostream> #include <cstring& ...

  5. hdu 1160 FatMouse's Speed (最长上升子序列+打印路径)

    Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove th ...

  6. UVA624 CD,01背包+打印路径,好题!

    624 - CD 题意:一段n分钟的路程,磁带里有m首歌,每首歌有一个时间,求最多能听多少分钟的歌,并求出是拿几首歌. 思路:如果是求时常,直接用01背包即可,但设计到打印路径这里就用一个二维数组标记 ...

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

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

  8. UVA 10054 The Necklace(欧拉回路,打印路径)

    题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  9. AOE网上的关键路径(最长路径 + 打印路径)

    题目描述 一个无环的有向图称为无环图(Directed Acyclic Graph),简称DAG图.     AOE(Activity On Edge)网:顾名思义,用边表示活动的网,当然它也是DAG ...

随机推荐

  1. Python学习笔记(七)加密加盐

    MD5加密和加盐 Python的MD5加密 Python的hashlib模块的MD5加密,是比较简单一种加密,md5函数必须传入编译后的结果,否则会报错: Traceback (most recent ...

  2. V4L2学习(三)框架分析

    整个v4l2的框架分为三层: 在应用层,我们可以在 /dev 目录发现 video0 类似的设备节点,上层的摄像头程序打开设备节点进行数据捕获,显示视频画面.设备节点的名字很统一,video0 vid ...

  3. python 二——函数、装饰器、生成器、面向对象编程(初级)

    本节内容 1.函数 2.装饰器 3.生成器 4.类 一.函数 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发“更快更好更强...” 函数式 ...

  4. loj6392 「THUPC2018」密码学第三次小作业 / Rsa

    还是挺好做的,\((e_1,e_2)=1 \Rightarrow e_1s+e_2t=0\),\(m \equiv m^1 \equiv m^{e_1s+e_2t} \equiv c_1^s c_2^ ...

  5. 五分钟搞定Linux容器

    [TechTarget中国原创] Linux容器针对特定工作负载提供了全新的灵活性与可能性.存在很多解决方案,但是没有一个解决方案能够像systemd容器那样进行快速部署.给我五分钟,本文将介绍如何使 ...

  6. uncompyle2反编译python的.py文件

    前几天学用github,一不小心把a.py文件给删除了,由于1天没有提交,也无法找回.突然发现同a.py文件生成的编译文件a.pyc还在,逐去搜索一番反编译的方法. 查询得知python比较好的工具u ...

  7. sql2005 和sql2008 同时安装

    Hkey_local_machine\Software\Wow6432Node\Microsoft\Microsoft SQL Server\90\Tools\ShellSEM 中的ShellSEM重 ...

  8. Ant 概念

    Ant的概念 可能有些读者并不连接什么是Ant以及入可使用它,但只要使用通过Linux系统得读者,应该知道make这个命令.当编译Linux内核及一些软件的源程序时,经常要用这个命令.Make命令其实 ...

  9. 通过 purge_relay_logs 自动清理relaylog

    使用背景 线上物理备份任务是在从库上进行的,xtrabackup会在备份binlog的时候执行flush logs,relay-log会rotate到新的一个文件号,导致sql thread线程应用完 ...

  10. CLion 使用笔记

    我并不会写 CMakeFiles . 我要对拍,所以需要按下 Ctrl + F9 后同时编译 main.cpp 和 ref.cpp 两个文件,并生成 main.exe 和 ref.exe 以备进一步处 ...