码迷,mamicode.com
首页 > 其他好文 > 详细

【HDOJ】1385 Minimum Transport Cost

时间:2014-05-31 04:41:38      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

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

标签:c   style   class   blog   code   java   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!