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 学习之 ...
随机推荐
- 【Spring 核心】装配bean(三)XML配置
项目包结构: src/main/java com.bonc.pojo--|-CompactDisc.java (接口) |-SgtPeppers.java (实现类 实现 CompactDis ...
- 【Js应用实例】限制上传图片大小
需求:前端页面开发中,常遇到图片上传的需求,一般要求限制图片格式及大小. 说明:这里就把实现此功能所需代码贴出来,供同志们参考,也方便我以后复用. 要点:1.处理 类型type为file的输入元素的v ...
- c# 后台get post请求
//get请求 public static TResult Get<TResult>(string host, string url) { var httpClient = new Htt ...
- python中的collections.namedtuple
简介 collections.namedtuple是一个工厂方法,它可以动态的创建一个继承tuple的子类.跟tuple相比,返回的子类可以使用名称来访问元素. 使用方法 用一个例子来介绍: > ...
- sequelizejs中文文档(一)
英文文档 入门 安装 Sequelize 支持 NPM 和 Yarn 的安装. // Using NPM $ npm install --save sequelize # And one of the ...
- 史上最全前端面试题(含答案)-A篇
HTML+CSS1.对WEB标准以及W3C的理解与认识标签闭合.标签小写.不乱嵌套.提高搜索机器人搜索几率.使用外 链css和js脚本.结构行为表现的分离.文件下载与页面速度更快.内容能被更多的用户所 ...
- DNSmasq服务搭建
.c { background: #FEFEF2; padding: 30px } hr { border: 1px dotted #70C4EF } DNSmasq介绍 DNSmasq是一个小巧且方 ...
- pycharm激活
刚刚下载了2017.1版本专业版的pycharm,作为一个天朝开发者,自然是去找注册码了.转悠了一圈,那些注册码都已经失效了.看到一个有效的方法:把http://elporfirio.com:1017 ...
- C# 单例模式(Singleton Pattern)
(新手写博客,主要是对自己学习的归纳总结.会对很多小细节详解.) 单例模式的定义: 确保一个类只有一个实例,并提供一个全局访问点. 首先实例大家应该都明白就是类生成对象的过程简单的就是String s ...
- JS中的函数和BOM
文档注释:开头两个*.写在函数上方,在调用函数时, 可以看到文档中的描述信息:function aaa(){ } [函数的声明及调用] 1.函数的声明格式: function 函数名(参数1,参数2, ...