hdu 1690 Bus System

http://acm.hdu.edu.cn/showproblem.php?pid=1690

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #define maxn 500
 7 #define LL __int64
 8 using namespace std;
 9 const LL inf=999999999999;
10 
11 LL L1,L2,L3,L4,C1,C2,C3,C4;
12 LL g[maxn][maxn];
13 LL a[maxn];
14 int n,m;
15 LL st,ed;
16 
17 LL Abs(LL x)
18 {
19     if(x<0) return -x;
20     return x;
21 }
22 
23 LL judge(LL x)
24 {
25     if(x>0&&x<=L1) return C1;
26     else if(x>L1&&x<=L2) return C2;
27     else if(x>L2&&x<=L3) return C3;
28     else if(x>L3&&x<=L4) return C4;
29     else return inf;
30 }
31 
32 void floyd()
33 {
34     for(int k=0; k<n; k++)
35     {
36         for(int i=0; i<n; i++)
37         {
38             if(g[i][k]==inf) continue;
39             for(int j=0; j<n; j++)
40             {
41                 if(g[i][j]>g[i][k]+g[k][j])
42                     g[i][j]=g[i][k]+g[k][j];
43             }
44         }
45     }
46 }
47 
48 int main()
49 {
50     int t;
51     scanf("%d",&t);
52     for(int case1=1; case1<=t; case1++)
53     {
54         cin>>L1>>L2>>L3>>L4>>C1>>C2>>C3>>C4;
55         cin>>n>>m;
56         for(int i=0; i<n; i++)
57         {
58             for(int j=0; j<n; j++)
59             {
60                 if(i==j) g[i][j]=0;
61                 else g[i][j]=inf;
62             }
63         }
64         for(int i=0; i<n; i++)
65         {
66             cin>>a[i];
67         }
68         for(int i=0; i<n; i++)
69         {
70             for(int j=i+1; j<n; j++)
71             {
72                 g[i][j]=g[j][i]=judge(Abs(a[i]-a[j]));
73             }
74         }
75         floyd();
76         printf("Case %d:\n",case1);
77         for(int i=0; i<m; i++)
78         {
79             cin>>st>>ed;
80             if(g[st-1][ed-1]!=inf)
81             printf("The minimum cost between station %I64d and station %I64d is %I64d.\n",st,ed,g[st-1][ed-1]);
82             else
83                 printf("Station %I64d and station %I64d are not attainable.\n",st,ed);
84         }
85     }
86     return 0;
87 }
View Code

 

posted @ 2014-05-06 19:43  null1019  阅读(140)  评论(0编辑  收藏  举报