Minimum Transport Cost(floyd+二维数组记录路径)

本文详细解析了一个关于寻找城市间最低运输成本路径的问题,并通过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


Problem Description
These are N cities in Spring country. Between each pair of cities there may be one transportation track or none. Now there is some cargo that should be delivered from one city to another. The transportation fee consists of two parts:
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
First is N, number of cities. N = 0 indicates the end of 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
From c to d :
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
5 0 3 22 -1 4 3 0 5 -1 -1 22 5 0 9 20 -1 -1 9 0 4 4 -1 20 4 0 5 17 8 3 1 1 3 3 5 2 4 -1 -1 0
 

 

Sample Output
From 1 to 3 : Path: 1-->5-->4-->3 Total cost : 21 From 3 to 5 : Path: 3-->4-->5 Total cost : 16 From 2 to 4 : Path: 2-->1-->5-->4 Total cost : 17
题解:坑了好久,首先就是城市的收费问题,首尾不加,只加中间;其次路径输出,竟然要用二维数组记录路径,一维为毛就不行,再者竟然要字典序输出。。。我类个去啊,逗了一比,长个记性,以后对于二维的数组就二维坐标记录路径,还有递归输出路径也要熟练。。。
代码:
 1 #include<stdio.h>
 2 #include<string.h>
 3 const int INF=0x3f3f3f3f;
 4 const int MAXN=1010;
 5 int map[MAXN][MAXN];
 6 int N,pre[MAXN][MAXN],cost[MAXN];
 7 void initial(){
 8     for(int i=1;i<=N;i++)
 9         for(int j=1;j<=N;j++){
10                 pre[i][j]=j;//初始化
11             if(i-j)map[i][j]=INF;
12                 else map[i][j]=0;
13         }
14 }
15 void floyd(){
16     for(int k=1;k<=N;k++)
17         for(int i=1;i<=N;i++)
18             for(int j=1;j<=N;j++)
19                 /*if(i==k||j==k)continue;
20             else*/ if(map[i][j]>map[i][k]+map[k][j]+cost[k]){
21             map[i][j]=map[i][k]+map[k][j]+cost[k];//就加中间
22             pre[i][j]=pre[i][k];
23             }
24             else if(map[i][j]==map[i][k]+map[k][j]+cost[k])
25                 if(pre[i][j]>pre[i][k])
26                 pre[i][j]=pre[i][k];//字典序输出。。。。
27 }
28 void print(int x,int y){
29     if(x==y){
30             printf("%d\n",y);
31         return ;
32     }
33     printf("%d-->",x);
34         print(pre[x][y],y);
35 }
36 int main(){
37     int a,b,c;
38     while(~scanf("%d",&N),N){
39         initial();
40         for(int i=1;i<=N;i++)
41             for(int j=1;j<=N;j++){
42                 scanf("%d",&a);
43                 if(a==-1)map[i][j]=INF;
44                 else map[i][j]=a;//这里错了我半天。。。。以前if(j>i)这样的就wa
45             }
46             for(int i=1;i<=N;i++)scanf("%d",&cost[i]);
47             floyd();
48             while(scanf("%d%d",&a,&b),a!=-1&&b!=-1){
49                 printf("From %d to %d :\n",a,b);
50                 printf("Path: ");
51                 print(a,b);
52                 printf("Total cost : %d\n",map[a][b]);
53                 puts("");
54             }
55     }
56     return 0;
57 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值