hdu-1690 Bus System

本文提供了一道 HDU ACM 竞赛题目的解答思路及实现代码。该题目要求使用 Floyd 算法解决城市间的费用计算问题。通过设定不同的费用区间,实现了对任意两点间距离的成本估算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

//感觉好恶心 的一道题,题意是说在x轴有n个城市,城市间往返的话费与距离的关系告诉我们,因为需要多次询问任意两点的距离,所以用floyed。

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
const long long INF = 1e18;//必须要大
const int maxn = 105;
long long map[maxn][maxn];
long long L1, L2, L3, L4;
long long C1, C2, C3, C4;
int n;

long long cost(long long dis)
{
    if(dis > 0 && dis <= L1)return C1;
    if(dis > L1 && dis <= L2)return C2;
    if(dis > L2 && dis <= L3)return C3;
    if(dis > L3 && dis <= L4)return C4;
    return INF;
}

void floyd()
{
    int 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()
{
    int ncase, k=1;

    cin >> ncase;

    while(ncase--)
    {
        int m, a, b, i, j;
        long long f[maxn]={0};

        cin >> L1 >> L2 >> L3 >> L4;
        cin >> C1 >> C2 >> C3 >> C4;

        cin >> n >> m;

        for(i=1; i<=n; i++)
        for(j=1; j<=n; j++)
            map[i][j] = INF;

        for(i=1; i<=n; i++)
            cin >> f[i];

        for(i=1; i<n; i++)
        for(j=i+1; j<=n; j++)
            map[i][j] = map[j][i] = cost(abs(f[i]-f[j]));

        floyd();

        cout << "Case " << k++ << ":" <<endl;

        for(i=0; i<m; i++)
        {
            cin >> a >> b;

            if(map[a][b] != INF)
                cout <<"The minimum cost between station "<< a <<" and station "<< b <<" is "<< map[a][b] <<"."<<endl;
            else
                cout <<"Station "<< a <<" and station "<< b <<" are not attainable."<<endl;
        }
    }

    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值