ACM学习之路___HDU 1385(带路径保存的 Floyd)
Description
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
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
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
Sample Output
#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)的更多相关文章
- ACM学习之路___HDU 5723(kruskal + dfs)
Abandoned country Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s) ...
- ACM学习之路___HDU 2066 一个人的旅行
Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还 ...
- FastAPI 学习之路(八)路径参数和数值的校验
系列文章: FastAPI 学习之路(一)fastapi--高性能web开发框架 FastAPI 学习之路(二) FastAPI 学习之路(三) FastAPI 学习之路(四) FastAPI 学习之 ...
- ACM学习之路————一个大整数与一个小整数不得不说得的秘密
这个相对于两个大整数的运算来说,只能说是,low爆了. 只要利用好除法的性质,这类题便迎刃而解.O(∩_∩)O哈哈~ //大整数除一个int数 #include<iostream> #in ...
- ACM学习之路__HDU 1045
Fire Net Description : Suppose that we have a square city with straight streets. A map of a city is ...
- ACM学习之路
2018-10-18 11:03:00 今天开始踏上实现梦想的道路,希望自己不要懈怠. 坚持做简单的事,坚持下来就会变得不简单.
- FastAPI 学习之路(十三)Cookie 参数,Header参数
系列文章: FastAPI 学习之路(一)fastapi--高性能web开发框架 FastAPI 学习之路(二) FastAPI 学习之路(三) FastAPI 学习之路(四) FastAPI 学习之 ...
- FastAPI 学习之路(十四)响应模型
系列文章: FastAPI 学习之路(一)fastapi--高性能web开发框架 FastAPI 学习之路(二) FastAPI 学习之路(三) FastAPI 学习之路(四) FastAPI 学习之 ...
- FastAPI 学习之路(十九)处理错误
系列文章: FastAPI 学习之路(一)fastapi--高性能web开发框架 FastAPI 学习之路(二) FastAPI 学习之路(三) FastAPI 学习之路(四) FastAPI 学习之 ...
随机推荐
- C# 实例练习(第二天)
实例练习 1. 完成简单登录效果,设置用户登录账号密码,清空控制台,进入登录页面,请求用户输入账号.密码和验证码(随机产生),并判断用户输入的信息,给出相应的提示. C#代码如下: 主要知识点: (1 ...
- 学习CSS记录:选择符优先级
1.标有!important 关键字声明属性. 2.HTML中的CSS样式属性. 3.作者编辑的CSS文件模式属性. 4.用户设置的样式. 5.浏览器默认的样式. ------------------ ...
- 总结切面编程AOP的注解式开发和XML式开发
有段日子没有总结东西了,因为最近确实有点忙,一直在忙于hadoop集群的搭建,磕磕碰碰现在勉强算是能呼吸了,因为这都是在自己的PC上,资源确实有点紧张(搭建过程后期奉上),今天难得大家都有空(哈哈哈~ ...
- 安全终端模拟工具Xshell 5使用密钥认证登录配置详细教程
▲版权声明:本文为博主原创文章,未经博主允许不得转载. Xshell支持SSH1 / SSH2协议,密码和DSA / RSA公钥用户认证方式等各种安全功能,并对各种加密算法进行加密.使用内置的Xshe ...
- The Karting 2017ccpc网络赛 1008
The Karting championship will be held on a straight road. There are N keypoints on the road. The pat ...
- Java获取Object属性值
做了一个拦截参数的需求,需要获取普通参数和对象参数 参数是Object类型,Object[] paramValues = pjp.getArgs(); 1.获取普通参数 ;i<paramValu ...
- Git学习总结(一)
.常用Git命令清单 一.新建代码库 #在当前目录创建一个Git代码库 $ git init #新建一个目录,将其初始化为Git代码库 $ git init [project name] #下载一个项 ...
- 栈的实现Java
package practice; import java.util.Iterator; //栈 public class MyStack<T> implements Iterable&l ...
- plsql developer 恢复默认布局界面
tools-preferences-appearance-(reset docking,reset toolbars)
- Spark 贝叶斯分类算法
一.贝叶斯定理数学基础 我们都知道条件概率的数学公式形式为 即B发生的条件下A发生的概率等于A和B同时发生的概率除以B发生的概率. 根据此公式变换,得到贝叶斯公式: 即贝叶斯定律是关于随机事件A和B ...