Minimum Transport Cost(floyd+二维数组记录路径)
Minimum Transport Cost
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8860 Accepted Submission(s): 2331
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.
#include<stdio.h>
#include<string.h>
const int INF=0x3f3f3f3f;
const int MAXN=;
int map[MAXN][MAXN];
int N,pre[MAXN][MAXN],cost[MAXN];
void initial(){
for(int i=;i<=N;i++)
for(int j=;j<=N;j++){
pre[i][j]=j;//初始化
if(i-j)map[i][j]=INF;
else map[i][j]=;
}
}
void floyd(){
for(int k=;k<=N;k++)
for(int i=;i<=N;i++)
for(int j=;j<=N;j++)
/*if(i==k||j==k)continue;
else*/ if(map[i][j]>map[i][k]+map[k][j]+cost[k]){
map[i][j]=map[i][k]+map[k][j]+cost[k];//就加中间
pre[i][j]=pre[i][k];
}
else if(map[i][j]==map[i][k]+map[k][j]+cost[k])
if(pre[i][j]>pre[i][k])
pre[i][j]=pre[i][k];//字典序输出。。。。
}
void print(int x,int y){
if(x==y){
printf("%d\n",y);
return ;
}
printf("%d-->",x);
print(pre[x][y],y);
}
int main(){
int a,b,c;
while(~scanf("%d",&N),N){
initial();
for(int i=;i<=N;i++)
for(int j=;j<=N;j++){
scanf("%d",&a);
if(a==-)map[i][j]=INF;
else map[i][j]=a;//这里错了我半天。。。。以前if(j>i)这样的就wa
}
for(int i=;i<=N;i++)scanf("%d",&cost[i]);
floyd();
while(scanf("%d%d",&a,&b),a!=-&&b!=-){
printf("From %d to %d :\n",a,b);
printf("Path: ");
print(a,b);
printf("Total cost : %d\n",map[a][b]);
puts("");
}
}
return ;
}
Minimum Transport Cost(floyd+二维数组记录路径)的更多相关文章
- hdu 1385 Minimum Transport Cost (Floyd)
Minimum Transport CostTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- Minimum Transport Cost Floyd 输出最短路
These are N cities in Spring country. Between each pair of cities there may be one transportation tr ...
- ZOJ 1456 Minimum Transport Cost(Floyd算法求解最短路径并输出最小字典序路径)
题目链接: https://vjudge.net/problem/ZOJ-1456 These are N cities in Spring country. Between each pair of ...
- HDU 1385 Minimum Transport Cost( Floyd + 记录路径 )
链接:传送门 题意:有 n 个城市,从城市 i 到城市 j 需要话费 Aij ,当穿越城市 i 的时候还需要话费额外的 Bi ( 起点终点两个城市不算穿越 ),给出 n × n 大小的城市关系图,-1 ...
- hdu 1385 Minimum Transport Cost(floyd && 记录路径)
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- nRF51800 蓝牙学习 进程记录 2:关于二维数组 执念执战
前天在玩OLED时想完成一直想弄得一个东西,就是简单的单片机游戏.因为STM32和nRF51822的内存足够,所以就用缓存数组的方法来显示图像(我也不知道术语是啥,反正就是在内存中建立一个128X64 ...
- HDU1385 Minimum Transport Cost (Floyd)
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- 个人学习记录1:二维数组保存到cookie后再读取
二维数组保存到cookie后再读取 var heartsArray = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0],[0,0, ...
- php 二维数组传递给 js 问题解决记录
需求: php从数据库中读取到二维数组.传递到js中 实现步骤: php:json_encode → json → js:eval 即在php中使用json_encode()将php的二维数 ...
随机推荐
- 解决:getWeatherbyCityName(city),服务器无法处理请求。 ---> 未将对象引用设置到对象的实例。
原文:getWeatherbyCityName(city),服务器无法处理请求. ---> 未将对象引用设置到对象的实例. 解决方法:不要直接使用 “服务引用” , 添加为 “Web 引用” 最 ...
- linux的NetworkManager服务(转)
在开启NetworkManager服务的情况下,在终端下敲“service network restart”命令: 正在关闭接口 eth0: 设备状态:3 (断开连接) [确定] 正在关闭接口 eth ...
- 网路流程图 TCP/IP
- OpenStack cloudCompute glassary术语project,tenant,user
1,tenantA group of users, used to isolate access to Compute resources(一组用户,用于隔离访问计算资源). An alternati ...
- redhat5安装jdk6、eclipse和tomcat6
redhat5安装jdk6.eclipse和tomcat6 1.安装jdk6 首先下载jdk(jdk-6u13-linux-i586.bin),对于redhat5,本人强烈建议不要安装jdk7,因为有 ...
- 有用的BitConverter
肯定有用的到的转换,记录下来. ///double =>ieee754 double d = 0.12345; byte[] IEEE754 = BitConverter.GetBytes(d) ...
- Java基础笔记-多线程
线程: 方式一:继承Thread类并且复写run方法. 格式: class MyThread extends Thread { public void run() { 线程中要运行的代码. } } 其 ...
- UVA 1610 Party Games
题意: 给出一系列字符串,构造出一个字符串大于等于其中的一半,小于另一半. 分析: 取大小为中间的两个a,b(a<b).实际上就是找出第一个小于b的同时大于等于a的字符串,直接构造即可. 代码: ...
- Python同步数据库的数据到Neo4J
写了主要是步骤,如果疑问,请咨询QQ:5988628 Python版本采用2.7.X,默认的2.6.X后期会有问题,建议,一开始就升级Python.然后再安装pip. 访问数据库 sqlalchemy ...
- python基础之python中if __name__ == '__main__': 的解析
当你打开一个.py文件时,经常会在代码的最下面看到if __name__ == '__main__':,现在就来介 绍一下它的作用. 模块是对象,并且所有的模块都有一个内置属性 __name__.一个 ...