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  
  简单带路径保存的Floyd(新手没啥经验,不熟练,记下方便回味)
 #include <iostream>
#include <cstdio>
using namespace std;
const int MAXINT = <<;
int path[][];
int num[][];
int num1[];
int n;
void floyd()
{
for(int k= ; k<=n ; k++)
for(int i= ; i<=n ; i++)
for(int j= ; j<=n ; j++)
{
if(num[i][j] > (num[i][k]+num[k][j]+num1[k]) )
{
num[i][j] = num[i][k]+num[k][j]+num1[k];
path[i][j] = path[i][k];
}
else if(num[i][j] == num[i][k]+num[k][j]+num1[k])
{
if(path[i][j] > path[i][k])
{
path[i][j] = path[i][k];
num[i][j] = num[i][k]+num[k][j]+num1[k];
}
}//attention 当存在多条最短路时,选取后驱小的路
}
}
void print(int a,int b)
{
if(a == b)
{
printf("%d\n",b);
return;
}
printf("%d-->",a);
a=path[a][b];
print(a,b);
}//路径输出函数 int main()
{
int i,j,a,b;
while(scanf("%d",&n),n)
{
for(i= ; i<=n ; i++)
for(j= ; j<=n ; j++)
{
scanf("%d",&num[i][j]);
if(num[i][j]==-)
num[i][j]=MAXINT;
path[i][j]=j;//记后驱,方便输出
}
for(i= ; i<=n ; i++)
scanf("%d",&num1[i]);
floyd();
while(scanf("%d%d",&a,&b))
{
if(a == - && b == -)
break;
printf("From %d to %d :\n",a,b);
printf("Path: ");
print(a,b);
printf("Total cost : %d\n\n",num[a][b]);
}
}
return ;
}
 
 

ACM学习之路___HDU 1385(带路径保存的 Floyd)的更多相关文章

  1. ACM学习之路___HDU 5723(kruskal + dfs)

    Abandoned country Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s) ...

  2. ACM学习之路___HDU 2066 一个人的旅行

    Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还 ...

  3. FastAPI 学习之路(八)路径参数和数值的校验

    系列文章: FastAPI 学习之路(一)fastapi--高性能web开发框架 FastAPI 学习之路(二) FastAPI 学习之路(三) FastAPI 学习之路(四) FastAPI 学习之 ...

  4. ACM学习之路————一个大整数与一个小整数不得不说得的秘密

    这个相对于两个大整数的运算来说,只能说是,low爆了. 只要利用好除法的性质,这类题便迎刃而解.O(∩_∩)O哈哈~ //大整数除一个int数 #include<iostream> #in ...

  5. ACM学习之路__HDU 1045

    Fire Net Description : Suppose that we have a square city with straight streets. A map of a city is ...

  6. ACM学习之路

     2018-10-18 11:03:00 今天开始踏上实现梦想的道路,希望自己不要懈怠. 坚持做简单的事,坚持下来就会变得不简单.

  7. FastAPI 学习之路(十三)Cookie 参数,Header参数

    系列文章: FastAPI 学习之路(一)fastapi--高性能web开发框架 FastAPI 学习之路(二) FastAPI 学习之路(三) FastAPI 学习之路(四) FastAPI 学习之 ...

  8. FastAPI 学习之路(十四)响应模型

    系列文章: FastAPI 学习之路(一)fastapi--高性能web开发框架 FastAPI 学习之路(二) FastAPI 学习之路(三) FastAPI 学习之路(四) FastAPI 学习之 ...

  9. FastAPI 学习之路(十九)处理错误

    系列文章: FastAPI 学习之路(一)fastapi--高性能web开发框架 FastAPI 学习之路(二) FastAPI 学习之路(三) FastAPI 学习之路(四) FastAPI 学习之 ...

随机推荐

  1. 【Linux SELinux】提升系统安全(一)

    本文重点:了解SELinux并能够熟练地启动关闭selinux(就像精通windows系统开关机一样) 背景:在centos5.x之后,selinux 非常完备地成为了系统内核模块,centos5.x ...

  2. vue调试工具vue-devtools安装及使用

    本文主要介绍 vue的调试工具 vue-devtools 的安装和使用 工欲善其事, 必先利其器, 快快一起来用vue-devtools来调试开发你的vue项目吧 安装:  1.到github下载: ...

  3. HTML DOM (文档对象模型)

    当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model). HTML DOM 模型被构造为对象的树. HTML DOM 树 通过可编程的对象模型,JavaScrip ...

  4. .NET Core+Selenium+Github+Travis CI => SiteHistory

    前言 总是三分钟热度的我折腾了一个可以每天自动截取指定网站页面并保存到Github的项目SiteHistory,感觉挺好(每次都这样). 想知道YouTube今天的首页长啥样么?点此查看 想知道You ...

  5. 如何获取Azure Storage Blob的MD5值

    问题表述 直接使用CloudBlockBlob对象获取的Properties是空的,无法获取到对象的MD5值,后台并未进行属性值的填充 前提:blob属性本省包含md5值,某些方式上传的blob默认并 ...

  6. archlinux系统安装博通B43XX系列无线网卡驱动

    我的无线网卡是博通的B43xx系列,大家都知道博通对于其Wifi卡在 GNU/Linux 上的支持不好可谓是臭名昭著. 用 lspci -vnn -d 14e4: 或者 lspci -vnn | gr ...

  7. <经验杂谈>前端form提交导出数据

    之前在做列表的是总会遇到一些导出的功能,而在做导出的时候总是习惯于用get的方法将参数放在url上,这样一来就会有很多的弊端,一是url的参数长度有限,遇到有的参数很长的时候就会报错,二是也不太安全. ...

  8. struts2--Action

    HTTP请求 提交 Struts2 StrutsPrepareAndExecuteFilter 核心控制器 -- 请求分发给不同Action Action书写的的三种格式 第一种 Action可以是 ...

  9. 在Intellij IDEA中使用Debug

    Debug用来追踪代码的运行流程,通常在程序运行过程中出现异常,启用Debug模式可以分析定位异常发生的位置,以及在运行过程中参数的变化.通常我们也可以启用Debug模式来跟踪代码的运行流程去学习三方 ...

  10. Bootstrap-table事件使用

    HTML <div class="alert alert-danger" id="eventInfo"></div> <table ...