Bus System

题目链接:                                                           http://acm.hdu.edu.cn/showproblem.php?pid=1690


View Code
# include<stdio.h>
# include<string.h>
#define Max 0x7fffffffffffff///第一不用“;”,第二,这是64位,是个很大的数
__int64 L1,L2,L3,L4,C1,C2,C3,C4;///这里尽量用__int64
__int64 map[120][120]; ///
__int64 point[120]; ///
int n,m;
__int64 s,e,len; ///
int main()
{
int k1,ncase,i,j,k;
scanf("%d",&ncase);
for(k1=1;k1<=ncase;k1++)
{
scanf("%I64d%I64d%I64d%I64d",&L1,&L2,&L3,&L4);
scanf("%I64d%I64d%I64d%I64d",&C1,&C2,&C3,&C4);
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
scanf("%I64d",&point[i]);
for(i=0;i<n;i++) ////这里就是算出任意2站点之间的费用的地方,存在了map[][]中;
{
map[i][i]=0;
for(j=i+1;j<n;j++)
{
len=point[i]-point[j];
if(len<0)
len=-len;
if(len<=L1)
{
map[i][j]=C1;///这里尽量分开写;
map[j][i]=C1;
}
else if(len<=L2)
{
map[i][j]=C2;
map[j][i]=C2;
}
else if(len<=L3)
{
map[i][j]=C3;
map[j][i]=C3;
}
else if(len<=L4)
{
map[i][j]=C4;
map[j][i]=C4;
}
else
{
map[i][j]=Max;
map[j][i]=Max;
}
}
}
for(k=0;k<n;k++) ////floyd的算法的,很简短,3个for循环,但能够把map[][]全部更新到最优
for(j=0;j<n;j++)
for(i=0;i<n;i++)
if(map[j][i]>map[j][k]+map[k][i])///这就是仅需处理的地方
map[j][i]=map[j][k]+map[k][i];
printf("Case %d:\n",k1);
for(i=1;i<=m;i++)
{
scanf("%I64d%I64d",&s,&e);
if(map[s-1][e-1]==Max) ////由于上面是从0开始的,这里要减1;
printf("Station %I64d and station %I64d are not attainable.\n",s,e);
else
printf("The minimum cost between station %I64d and station %I64d is %I64d.\n",s,e,map[s-1][e-1]);
}
}
return 0;
}

  

posted on 2011-08-13 09:31  world_ding  阅读(206)  评论(0编辑  收藏  举报