*HDU 1385 最短路 路径
Minimum Transport Cost
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10183 Accepted Submission(s): 2791
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.
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:
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.
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 用一个path数组记录路径。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int mp[][];
int fei[],path[][];
int main()
{
int n,a,b,t;
while(scanf("%d",&n)&&n)
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
scanf("%d",&mp[i][j]);
if(mp[i][j]==-)
mp[i][j]=;
path[i][j]=j; //初始化路径
}
for(int i=;i<=n;i++)
scanf("%d",&fei[i]);
for(int k=;k<=n;k++)
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
if(mp[i][j]>mp[i][k]+mp[k][j]+fei[k]) //加上tax.
{
mp[i][j]=mp[i][k]+mp[k][j]+fei[k];
path[i][j]=path[i][k];
}
else if(mp[i][j]==mp[i][k]+mp[k][j]+fei[k]&&path[i][j]>path[i][k])
path[i][j]=path[i][k];
}
while(scanf("%d%d",&a,&b))
{
if(a<&&b<) break;
printf("From %d to %d :\n",a,b);
int x=a;
printf("Path: %d",a);
while(x!=b)
{
printf("-->%d",path[x][b]);
x=path[x][b];
}
printf("\n");
printf("Total cost : %d\n\n",mp[a][b]);
}
}
return ;
}
*HDU 1385 最短路 路径的更多相关文章
- 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 // ...
- hdu 1385 floyd记录路径
可以用floyd 直接记录相应路径 太棒了! http://blog.csdn.net/ice_crazy/article/details/7785111 #include"stdio.h& ...
- hdu 1385(Floyed+打印路径好题)
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- ACM: HDU 2544 最短路-Dijkstra算法
HDU 2544最短路 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descrip ...
- UESTC 30 &&HDU 2544最短路【Floyd求解裸题】
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu 5521 最短路
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- 堆优化Dijkstra计算最短路+路径计数
今天考试的时候遇到了一道题需要路径计数,然而蒟蒻从来没有做过,所以在考场上真的一脸懵逼.然后出题人NaVi_Awson说明天考试还会卡SPFA,吓得我赶紧又来学一波堆优化的Dijkstra(之前只会S ...
- CodeForces - 449B 最短路(迪杰斯特拉+堆优化)判断最短路路径数
题意: 给出n个点m条公路k条铁路. 接下来m行 u v w //u->v 距离w 然后k行 v w //1->v 距离w 如果修建了铁路并不影响两点的最短距离, ...
- HDU - 2544最短路 (dijkstra算法)
HDU - 2544最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以 ...
随机推荐
- 关闭Linux防火墙(iptables) 及 SELinux
一.关闭防火墙 1.重启后永久性生效: 开启:chkconfig iptables on 关闭:chkconfig iptables off 2.即时生效,重启后失效: 开启:service ipta ...
- 关于vue指令(directive)
1.指令的注册 指令跟组件一样需要注册才能使用,同样有两种方式,一种是全局注册: Vue.directive('dirName',function(){ //定义指令 }); 另外一种是局部注册: n ...
- ReactiveCocoa源码拆分解析(四)
(整个关于ReactiveCocoa的代码工程可以在https://github.com/qianhongqiang/QHQReactive下载) 上一章节简要的说明了如何实现的热信号.但是像那么写, ...
- MFC在关闭第二个窗口时关闭主对话框
AfxGetApp()->m_pMainWnd->SendMessage(WM_CLOSE);//关闭主对话框
- C和指针 第六章 数组名与指针
指针的算术运算符是指针和数组之间的一种关联,但不是唯一关联: 可以使用数组名作为指向数组第一个元素的指针,但是不可以给数组名赋新的值. //如下声明a int a[10]; //用a作为指向数组第一个 ...
- Linux 架构
(转)作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! http://www.cnblogs.com/vamei/archive/2 ...
- angular-ui-bootstrap-modal必须要说的几个点(转)
angular-ui-bootstrap-modal必须要说的几个点 摘要: 基于angular来实现的一个bootstrap模态框,有些不得不说的地方 项目中以前就经常用到模态框,但是一直没有时间来 ...
- WORDPRESS点击标题或图片无法链接到文章页面
在设置出更改固定连接设置
- Python dir
1. 在python命令行交互环境下,可以用dir()函数查看当前的变量,比如: >>> dir()['__builtins__', '__doc__', '__loader__', ...
- nginx实现本地图片生成缩略图
nginx可以实现图片的缩略图效果,很多网站为了前端静态资源相应的性能会给大图自动生成一个小图,比如我们经常会在网上看到bd_64x64.png这种格式,淘宝上的小图经常会看到xxx.jpg_100x ...