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 学习之 ...
随机推荐
- JDK安装图解
JDK安装部分.. ---------- 此处是一个JDK下载链接:http://www.cr173.com/soft/14290.html 首先是解压下载的JDK安装包.然后右键以管理员身份运行. ...
- vue.js基础知识篇(5):过渡、Method和Vue实例方法
第8章:过渡 1.CSS过渡 2.JavaScript过渡 3.渐进过渡 第9章:method Vue.js的事件一般通过v-on指令配置在HTML中,虽然也可以在js的代码中使用原生的addEven ...
- linux ls 命令
ls 命令是 Linux 下最常用的命令之一,用来查询目录下的内容(list directory contents).本文将介绍其基本的用法和一些典型的用例.笔者使用的测试环境为 ubuntu 16. ...
- Ext:ComboBox实战
var gsCombo = new Ext.form.ComboBox({ id:'combo', hiddenName:'value', fieldLabel:'下拉框', triggerActio ...
- java TreeSet应用
这篇是紧接着上一篇而写的,具体的实现TreeSet中有序的第二中方法 首先新建一个类,此类就是用于集合中存放的对象 然后定义一个类,实现Comparator中的CompareTo()方法 最后一个测试 ...
- java值传递与引用传递实例
public class Test2 { public static void main(String[] args) { int[] arr=new int[5]; arr[0]=10; arr[1 ...
- C++中值传递、指针传递、引用传递的总结
C++中值传递.指针传递.引用传递的总结 指针传递和引用传递一般适用于:函数内部修改参数并且希望改动影响调用者.对比值传递,指针/引用传递可以将改变由形参"传给"实参(实际上就 ...
- JVM启动参数设置
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt174 不管是YGC还是Full GC,GC过程中都会对导致程序运行中中断,正 ...
- java读取txt文件内容
package read; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; public ...
- yum安装mariadb-galera同步
节点 ip地址 hostname 系统版本 程序版本 node1 10.4.90.90 mysql1 db1 ...