HDU 1690 Bus System(多源最短路径)

HDU 1690 题解
本文提供了一道 HDU 1690 的算法题解,使用 C++ 实现,并通过 Floyd 算法求解最短路径问题。详细介绍了程序结构、关键函数 abss 的实现方式及如何进行距离矩阵更新。

http://acm.hdu.edu.cn/showproblem.php?pid=1690

无穷大设置的不好,WA了3次,最后干脆改成-1过了。

#include <cstdio>
#include <iostream>
#include  <cstring>
using namespace std;
const int maxn = 100+5;
const __int64 inf=-1;
__int64 map[maxn][maxn],dist[maxn][maxn];
__int64 L[5],C[5];
__int64 x[maxn];
__int64 abss(__int64 x)
{
    if(x<0) x=x*(-1);
    if(x<=L[1]) return C[1];
    else if(x<=L[2]) return C[2];
    else if(x<=L[3]) return C[3];
    else if(x<=L[4]) return C[4];
    return inf;
}
int n;
void floyd()
{
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++){
           if(i==j) dist[i][j]=0;
           else dist[i][j]=map[i][j];
        }
    for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(dist[i][k]!=-1&&dist[k][j]!=-1&&(dist[i][j]>dist[i][k]+dist[k][j]||dist[i][j]==-1))
                    dist[i][j]=dist[i][k]+dist[k][j];
}
int main()
{
    int T,m;
    scanf("%d",&T);
    for(int kas=1;kas<=T;kas++)
    {

        for(int i=1;i<5;i++) scanf("%I64d",&L[i]);
        for(int i=1;i<5;i++) scanf("%I64d",&C[i]);
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++){
            scanf("%I64d",&x[i]);
            for(int j=1;j<i;j++)
               map[i][j]=map[j][i]=abss(x[i]-x[j]);
        }
        floyd();
        printf("Case %d:\n",kas);
        while(m--)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            if(dist[u][v]!=-1)
                printf("The minimum cost between station %d and station %d is %I64d.\n",u,v,dist[u][v]);
            else
                printf("Station %d and station %d are not attainable.\n",u,v);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值