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的二维数 ...
随机推荐
- Zigbee2007协议中文介绍
Zigbee2007中文介绍ZigBee2007规范定义了ZigBee和ZigBee Pro两个特性集,全新的ZigBee 2007规范建立在ZigBee2006之上,不但提供了增强型的功能而且在某些 ...
- codec ruby和json格式输出
zjtest7-frontend:/usr/local/logstash-2.3.4/config# cat geoip.conf input {stdin {} } filter { geoip { ...
- CC++初学者编程教程(15) 基于cocos2dx的安卓打包环境
1首先安装python 2 单击next 3 选择默认路径,单击next 4选择完全安装,单击next 5单击next开始安装 6 安装完成 7 设置环境变量 8 添加python的路径到path 9 ...
- QT 绘制按钮 paintEvent enterEvent leaseEvent mouseEvent
案例2:绘制按钮 main.cpp #include<QApplication> #include “demoWidget.h” int main(int args , int arg ...
- [置顶] 获取系统时间的方法--linux
一. localtime 函数获取(年/月/日/时/分/秒)数值. 1.感性认识 #include<time.h> //C语言的头文件 #include<stdio.h> ...
- 我使用过的Linux命令之file - 检测并显示文件类型
摘自:http://codingstandards.iteye.com/blog/804463 我使用过的Linux命令之file - 检测并显示文件类型 用途说明 file命令是用来检测并显示文件类 ...
- 命令行运行命令时报错You don't have write permissions for the /Library/***
这是由于要运行这些操作时必须有管理员的权限(比方更新软件),比方更新cocoapods时报错 soindy:SmartThermo soindy$ gem install cocoapods Fetc ...
- 如何使用Storyboard创建UIPageViewController
之前我们已经讲过UIPageViewController,那篇文章演示了如何使用Interface Builder创建UIPageViewController.为了适配iOS7和Xcode5,我们重新 ...
- twitter 监控登陆活动
http://vicenteaguileradiaz.com/download/tinfoleak/tinfoleak-1.2.tar.gz
- Array数组方法的总结
添加元素: 1. push(arg1,arg2,arg3....) 向数组的尾部添加元素,返回值是数组的长度. 2.unshift(arg1,arg2,arg3....) 向数组的头部添加元素,返回 ...