Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3746 Accepted Submission(s): 943
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define N 2003
#define lson l,m,k<<1
#define rson m,r,k<<1|1
using namespace std;
__int64 map[103][103];
int index[103];
int main()
{
int T,t=1;
int l1,l2,l3,l4,c1,c2,c3,c4;
int m,n;
scanf("%d",&T);
int i,j,k;
int temp;
while(T--)
{
scanf("%d%d%d%d%d%d%d%d",&l1,&l2,&l3,&l4,&c1,&c2,&c3,&c4);
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
map[i][i]=0,scanf("%d",&index[i]);
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
{
temp=index[i]-index[j];
if(temp<0)
temp=-temp;
if(temp<=l1)
{
map[i][j]=map[j][i]=c1;
}
else if(l1<temp&&temp<=l2)
{
map[i][j]=map[j][i]=c2;
}
else if(l2<temp&&temp<=l3)
{
map[i][j]=map[j][i]=c3;
}
else if(l3<temp&&temp<=l4)
{
map[i][j]=map[j][i]=c4;
}
else {map[i][j]=map[j][i]=-1;}
}
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(map[i][k]!=-1&&map[k][j]!=-1)
if(map[i][j]>map[i][k]+map[k][j]||map[i][j]==-1)
map[i][j]=map[i][k]+map[k][j];
printf("Case %d:\n",t++);
while(m--)
{
scanf("%d%d",&i,&j);
if(map[i][j]!=-1)
{
printf("The minimum cost between station %d and station %d is %I64d.\n",i,j,map[i][j]);
}
else
{
printf("Station %d and station %d are not attainable.\n",i,j);
}
}
}
return 0;
}