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的二维数 ...
随机推荐
- MFC连接ftp服务器
CInternetSession* m_pInetSession; CFtpConnection* m_pFtpConnection; //连接服务器的函数 BOOL CftpClientDlg: ...
- Jquery基础之事件操作
事件是用户操作时页面或页面加载时引发的用来完成javascript和HTML之间的交互操作.常见的元素点击事件.鼠标事件.键盘输入事件等,较传Javascript 相比JQuery增加并扩展了基本的事 ...
- 检测android的版本的办法
http://www.cnblogs.com/wzh206/archive/2010/05/02/1726076.html 如何判断Android系统的版本 随着Android版本的增多,在不同的版本 ...
- USB 传输协议
Pipe USB的pipe的两端分别指的是USB host端的内存区域,和设备端的endpoint. pipe分为两类,一类是stream pipe, 另一类是message pipe. 两类的主要区 ...
- 并发(Concurrency)和并行(Parallelism)的区别
最近在读<real world haskell>里关于并行的一章时,看到作者首先对并发(Concurrency)和并行(Parallelism)的区别进行了定义和解释.以前我对这个问题也是 ...
- "NO 3D support is available from the host"
https://forums.opensuse.org/showthread.php/494522-No-3d-Support-or-graphics-accelleration http://ask ...
- JqueryeasyUI选项卡选择判定更改内部Iframe地址
1.tabs的常用操作 //1.判断tab是否存在. var currtab = $('#tabs').tabs('getSelected'); //2.判断点击的tab是否是当前选中的tab. va ...
- 续上文----线性表之单链表(C实现)
本文绪上文线性表之顺序表(C实现) 本文将继续使用单链表实现线性表的另外一种存储结构.这种使用链表实现的存储结构在内存中是不连续的. C实现代码如下: #include<stdio.h> ...
- poj 1149 PIGS_网络流
#include<iostream> #include<queue> #include<cstdio> #include<cstring> using ...
- E6全部刷机包
此版本号基于R533_G_11.11.10P_GSZMCAUT679DA01B_LP064DA_T679DA_S005_E001_P002_R001_G004_1FF.sbf制作耳机接听或挂机正常内置 ...