首页 > 其他 > 详细

【HDOJ】1385 Minimum Transport Cost

时间:2014-05-31 04:40:56      阅读:390      评论:0      收藏:0      [点我收藏+]

Floyd。注意字典序!!!

bubuko.com,布布扣
 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 #define MAXNUM 55
 5 #define INF 0x1fffffff
 6 
 7 int cost[MAXNUM][MAXNUM];
 8 int path[MAXNUM][MAXNUM];
 9 int taxes[MAXNUM];
10 int que[MAXNUM];
11 int n;
12 
13 void floyd(int n) {
14     int i, j, k, tmp;
15 
16     for (i=1; i<=n; ++i)
17         for (j=1; j<=n; ++j)
18             path[i][j] = j;
19 
20     for (k=1; k<=n; ++k) {
21         for (i=1; i<=n; ++i) {
22             for (j=1; j<=n; ++j) {
23                 tmp = cost[i][k]+cost[k][j]+taxes[k];
24                 if (cost[i][j] > tmp) {
25                     cost[i][j] = tmp;
26                     path[i][j] = path[i][k];
27                 } else if (cost[i][j] == tmp) {
28                     if (path[i][j] > path[i][k])
29                         path[i][j] = path[i][k];
30                 }
31             }
32         }
33     }
34 }
35 
36 void output(int a, int b) {
37     int front, rear;
38     int x = a;
39     front = rear = 0;
40 
41     //que[rear++] = a;
42     while (path[x][b] != b) {
43         que[rear++] = path[x][b];
44         x = path[x][b];
45     }
46     if (a != b)
47         que[rear++] = b;
48 
49     printf("From %d to %d :\n", a, b);
50     printf("Path: %d", a);
51     while (front < rear)
52         printf("-->%d", que[front++]);
53     printf("\nTotal cost : %d\n\n", cost[a][b]);
54 }
55 
56 int main() {
57     int a, b;
58     int i, j;
59 
60     while (scanf("%d", &n)!=EOF && n) {
61         for (i=1; i<=n; ++i)
62             for (j=1; j<=n; ++j) {
63                 scanf("%d", &cost[i][j]);
64                 if (cost[i][j] < 0)
65                     cost[i][j] = INF;
66             }
67         for (i=1; i<=n; ++i)
68             scanf("%d", &taxes[i]);
69         floyd(n);
70         while (1) {
71             scanf("%d %d", &a, &b);
72             if (a==-1 && b==-1)
73                 break;
74             output(a, b);
75         }
76     }
77 
78     return 0;
79 }
bubuko.com,布布扣

 

【HDOJ】1385 Minimum Transport Cost,布布扣,bubuko.com

【HDOJ】1385 Minimum Transport Cost

原文:http://www.cnblogs.com/bombe1013/p/3761666.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!