HDU_1690

    直接用Floyd算法求出任意两点之间的最少花费即可。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
long long int L1,L2,L3,L4,C1,C2,C3,C4;
long long int f[110][110],d[110];
int main()
{
int i,j,k,N,M,t,tt,a,b;
long long int temp;
scanf("%d",&t);
for(tt=0;tt<t;tt++)
{
scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d",
&L1,&L2,&L3,&L4,&C1,&C2,&C3,&C4);
scanf("%d%d",&N,&M);
for(i=0;i<N;i++)
scanf("%I64d",&d[i]);
for(i=0;i<N;i++)
for(j=0;j<N;j++)
{
if(i==j)
f[i][j]=0;
else
{
temp=abs(d[i]-d[j]);
if(temp<=L1)
f[i][j]=C1;
else if(temp>L1&&temp<=L2)
f[i][j]=C2;
else if(temp>L2&&temp<=L3)
f[i][j]=C3;
else if(temp>L3&&temp<=L4)
f[i][j]=C4;
else
f[i][j]=-1;
}
}
for(k=0;k<N;k++)
for(i=0;i<N;i++)
for(j=0;j<N;j++)
if(f[i][k]!=-1&&f[k][j]!=-1)
{
temp=f[i][k]+f[k][j];
if(temp<f[i][j]||f[i][j]==-1)
f[i][j]=temp;
}
printf("Case %d:\n",tt+1);
for(i=0;i<M;i++)
{
scanf("%d%d",&a,&b);
if(f[a-1][b-1]==-1)
printf("Station %d and station %d are not attainable.\n",a,b);
else
printf("The minimum cost between station %d and station %d is %I64d.\n",a,b,f[a-1][b-1]);
}
}
return 0;
}