HDU 1690---最短路

非常恶心的最短路。。我wa了一中午,因为VC的限制,long long运行不了,但是这题又需要定义一个大于int的常数,自己写了一段代码无限wa。。。

题目不算难,就是有点恶心。。

直接floyd暴力就可以水过。。不纠结,日后有空再写一遍。

附上代码:

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <memory.h>
 4 #include <cmath>
 5 using namespace std;
 6 
 7 const long long INF = 99999999999LL;    //注意!要加LL,不然会报错数据太大
 8 const int N = 105;
 9 
10 int l1, l2, l3, l4, c1, c2, c3, c4;
11 long long map[N][N];    //距离可能会爆int,所以用long long
12 int place[N];
13 int n, m;
14 
15 void init()
16 {
17     int i, j;
18     for(i = 1; i <= n; i++)
19         for(j = 1; j <= n; j++)
20             if(i == j) map[i][j] = 0;
21             else map[i][j] = INF;
22 }
23 
24 void input()
25 {
26     int i, j, len;
27     scanf("%d%d%d%d%d%d%d%d", &l1, &l2, &l3, &l4, &c1, &c2, &c3, &c4);
28     scanf("%d %d", &n, &m);
29     init();
30     for(i = 1; i <= n; i++)
31     {
32         scanf("%d", &place[i]);
33     }
34     for(i = 1; i <= n; i++)
35     {
36         for(j = i+1; j <= n; j++)
37         {
38             len = abs(place[i] - place[j]);
39             if(0 < len && len <= l1) map[i][j] = map[j][i] = c1;
40             else if(l1 < len && len <= l2) map[i][j] = map[j][i] = c2;
41             else if(l2 < len && len <= l3) map[i][j] = map[j][i] = c3;
42             else if(l3 < len && len <= l4) map[i][j] = map[j][i] = c4;
43         }
44     }
45 }
46 
47 void floyd()    //这题绝对是用floyd方便
48 {
49     int i, j, k;
50     for(k = 1; k <= n; k++)
51         for(i = 1; i <= n; i++)
52             for(j = 1; j <= n; j++)
53                 if(map[i][j] > map[i][k] + map[k][j])
54                      map[i][j] = map[i][k] + map[k][j];
55 }
56 
57 void output()
58 {
59     int ti, tj;
60     static int zz = 1;
61     printf("Case %d:\n", zz++);
62     while(m--)
63     {
64         scanf("%d %d", &ti, &tj);
65         if(map[ti][tj] != INF)
66             printf("The minimum cost between station %d and station %d is %I64d.\n", ti, tj, map[ti][tj]);
67         else
68             printf("Station %d and station %d are not attainable.\n", ti, tj);
69     }
70 }
71 
72 int main()
73 {
74     int t;
75     scanf("%d", &t);
76     while(t--)
77     {
78         input();
79         floyd();
80         output();
81     }
82 
83     return 0;
84 }

 

posted on 2013-02-04 20:15  acoderworld  阅读(49)  评论(0)    收藏  举报

导航