题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1690
2 1 2 3 4 1 3 5 7 4 2 1 2 3 4 1 4 4 1 1 2 3 4 1 3 5 7 4 1 1 2 3 10 1 4
Case 1: The minimum cost between station 1 and station 4 is 3. The minimum cost between station 4 and station 1 is 3. Case 2: Station 1 and station 4 are not attainable.
#include <cstdio>
#include <cstring>
#include <cmath>
#define INF 1e18
#define MAXN 117
__int64 dis[MAXN][MAXN];
__int64 L1, L2, L3, L4;
__int64 C1, C2, C3, C4;
__int64 f[MAXN];
int n, m;
__int64 min(__int64 a, __int64 b)
{
return a < b ? a:b;
}
void init()
{
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
dis[i][j] = INF;
}
}
}
void Floyd()
{
for(int k = 1; k <= n; k++)
{
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
dis[i][j] = min(dis[i][j],dis[i][k]+dis[k][j]);
}
}
}
}
__int64 cal(__int64 x)
{
if(x > 0 && x <= L1)
return C1;
if(x > L1 && x <= L2)
return C2;
if(x > L2 && x <= L3)
return C3;
if(x > L3 && x <= L4)
return C4;
return INF;
}
int main()
{
int t;
int i, j;
__int64 s, e;
int cas = 0;
scanf("%d",&t);
while(t--)
{
scanf("%I64d%I64d%I64d%I64d",&L1,&L2,&L3,&L4);
scanf("%I64d%I64d%I64d%I64d",&C1,&C2,&C3,&C4);
scanf("%d%d",&n,&m);
init();
memset(f,0,sizeof(f));
for(i = 1; i <= n; i++)
{
scanf("%I64d",&f[i]);
}
for(i = 1; i < n; i++)
{
for(j = i+1; j <= n; j++)
{
dis[i][j] = dis[j][i] = cal(fabs(f[i]-f[j]));
}
}
Floyd();
printf("Case %d:\n",++cas);
for(i = 1; i <= m; i++)
{
scanf("%I64d%I64d",&s,&e);
if(dis[s][e] != INF)
printf("The minimum cost between station %I64d and station %I64d is %I64d.\n",s,e,dis[s][e]);
else
printf("Station %I64d and station %I64d are not attainable.\n",s,e);
}
}
return 0;
}
hdu 1690 Bus System(Floyd),布布扣,bubuko.com
原文地址:http://blog.csdn.net/u012860063/article/details/38401219