简单题意:车票的价格决定于两个站点之间的距离。给出了描述距离和价钱之间关系的表格。求花费最少
解题思路:用Floyd算法,借鉴了网上的算法,他们用的是_int64,直接用int不知道有没有问题;
代码如下:
#include
#include
#include
using namespace std;
const __int64 inf = 1e18;
__int64 map[105][105];
__int64 k[105];
__int64 a[5];
__int64 c[5];
__int64 n,m;
__int64 judge(__int64 x)
{
if(x>0 && x<=a[1])
return c[1];
if(x<=a[2])
return c[2];
if(x<=a[3])
return c[3];
if(x<=a[4])
return c[4];
return inf;
}
__int64 kabs(__int64 a)
{
return a<0?-a:a;
}
void floyd()
{
__int64 i,j,k;
for(k = 1; k<=n; k++)
for(i = 1; i<=n; i++)
for(j = 1; j<=n; j++)
if(map[i][j]>map[i][k]+map[k][j])
map[i][j] = map[i][k]+map[k][j];
}
int main()
{
__int64 t,i,j,x,y,cas = 1;
scanf("%I64d",&t);
while(t--)
{
for(i = 1; i<=4; i++)
scanf("%I64d",&a[i]);
for(i = 1; i<=4; i++)
scanf("%I64d",&c[i]);
scanf("%I64d%I64d",&n,&m);
for(i = 1; i<=n; i++)
scanf("%I64d",&k[i]);
for(i = 0; i<=n; i++)
{
for(j = 0; j<=n; j++)
map[i][j] = inf;
map[i][i] = 0;
}
for(i = 1; i<=n; i++)
for(j = i; j<=n; j++)
{
map[i][j] = map[j][i] = judge(kabs(k[j]-k[i]));
}
floyd();
printf("Case %I64d:\n",cas++);
while(m--)
{
scanf("%I64d%I64d",&x,&y);
if(map[x][y] == inf)
printf("Station %I64d and station %I64d are not attainable.\n",x,y);
else
printf("The minimum cost between station %I64d and station %I64d is %I64d.\n",x,y,map[x][y]);
}
}
return 0;
}