P1016 [NOIP1999 提高组] 旅行家的预算

比较简单的普及加,贪心加模拟即可轻松解出

//C++输出代码的运行时间
/*
double starttime=clock();
double endtime=clock();
cout<<(double)(endtime-starttime)/CLOCK_PER_SEC;
CLOCK_PER_SEC表示一秒钟内CPU运行的时钟周期数
*/ 
/*
将每个油站包括初始油站按油价从小到大进行排序
每次到油站都以最大油量所能行驶距离去搜索此距离内下一个比自己便宜的油站
//如果搜索到了,则在当前油站加到刚好到达此油站所需要的油或不加油(油箱内的油超了的话)
//如果未搜索到比自己便宜的油站但搜索到此段距离中最便宜的油站(不包括当前油站)
且未搜索到终点则在当前油站把油箱加满
//如果未搜索到比自己便宜的油站但搜索到此段距离中最便宜的油站(不包括当前油站)
并搜索到终点则加油至刚好到终点
//如果未搜索到油站且未搜索到终点则输出No Solution
//如果未搜索到油站但搜索到终点,则加到刚好到终点的油
*/
#include<iostream>
#include<iomanip>
#include<cstring>
#include<time.h>
#include<cmath>
#include<queue>
using namespace std;
struct petrol_station
{
   double distance;//油站i距离出发点的距离di
   double prince;//油站i每升汽油价格pi
}ps[7];
double d1;//两个城市之间的距离
double c;//汽车油箱的容量
double d2;//每升汽油能行驶的距离
double p0;//出发点汽油价格
int n;//沿途油站数
double cost;
bool cmp(petrol_station a, petrol_station b)
{
    return a.prince < b.prince;
}
int main()
{
    ios::sync_with_stdio(false);//可关闭和stdio同步加速
    freopen("title.in", "r", stdin);
    cin >> d1 >> c >> d2 >> p0 >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> ps[i].distance >> ps[i].prince;
    }
    ps[0].distance = 0; ps[0].prince = p0;
    double max_distance = c * d2;
    double gasoling = 0;//当前车内油量
    petrol_station current;
    current.distance = 0;
    current.prince = p0;
    while (current.distance < d1)
    {
        int minp = 9999, minn = -1, nextn = -1;//nextn记录比当前油站便宜的下一个油站
        //minp为所能到达的距离中最小的油价,minn为所能到达的距离中最小油价的油站的位置
        for (int i = 1; i <= n; i++)
        {
            if (current.distance + max_distance >= ps[i].distance&&current.distance<ps[i].distance)
            {
                if (current.prince >= ps[i].prince)
                {
                    nextn = i;
                    break;
                }
                if (minp >= ps[i].prince)
                {
                    minp = ps[i].prince;
                    minn = i;
                }
            }

        }
        if (nextn != -1)
        {
            //以下步骤模拟了加油计算,加油,行驶,三种情况都是这样的模板
            double distance_nextcity = ps[nextn].distance - current.distance;//计算两地距离
            double gas = distance_nextcity / d2;//计算从当地到目标地所需油量
            if (gasoling < gas)
            {
                double money = current.prince * (gas-gasoling);//计算加所需油量所需要花费的钱
                cost += money;
                gasoling = gas;//给汽车加油
            }
            current.distance = ps[nextn].distance;//到达目的地,更改里程数和当地油价
            current.prince = ps[nextn].prince;
            gasoling -= gas;//消耗掉的油量
        }
        else if (nextn == -1 && minn != -1 && current.distance + max_distance < d1)
        {
            double distance_nextcity = ps[minn].distance - current.distance;
            double gas = distance_nextcity / d2;
            double money = (c - gasoling) * current.prince;
            gasoling = c;
            cost += money;
            current.distance = ps[minn].distance;
            current.prince = ps[minn].prince;
            gasoling -= gas;
        }
        else if (nextn == -1 && current.distance + max_distance >= d1)
        {
            double distance_nextcity = d1 - current.distance;
            double gas = distance_nextcity / d2;
            if (gasoling < gas)
            {
                double money = current.prince * (gas-gasoling);
                cost += money;
                gasoling = gas;
            }
            current.distance = d1;
            current.prince = -1;
            gasoling -= gas;
        }
        else
        {
            cout << "No Solution";
            return 0;
        }
    }
    cout << fixed << setprecision(2) << cost;
    return 0;
}

大佬写法

#include <bits/stdc++.h>
using namespace std;
#define maxn 100000
#define db double
#define INF 9999999 
int n;
db D1, D2, C, P, res, ans, maxx;

struct node
{
    db co, dis;
    bool friend operator <(const node& a, const node& b)
    { return a.dis < b.dis; }
}pl[maxn];

int Solve(int now)
{
    int flag = INF; db d = pl[now].dis; 
    for(int i = now + 1; i <= n && pl[i].dis - d <= maxx; i ++)
    {
        if(pl[i].co < pl[now].co)
        {
            ans += ((pl[i].dis - d - res) / D2) * pl[now].co;
            res = 0; return i;
        }
        if(flag == INF || pl[i].co < pl[flag].co) flag = i;
    }
    if(D1 - pl[now].dis <= maxx)
    {
        ans += ((D1 - pl[now].dis - res) / D2) * pl[now].co;
        return INF;
    }
    if(flag == INF) { printf("No Solution\n"); return -1; }
    else
    {
        ans += C * pl[now].co; res += (maxx - (pl[flag].dis - d));
        return flag;
    }
}

int main()
{
    scanf("%lf%lf%lf%lf%d", &D1, &C, &D2, &P, &n);
    pl[0].dis = 0, pl[0].co = P;
    for(int i = 1; i <= n; i ++) 
        scanf("%lf%lf", &pl[i].dis, &pl[i].co);
    sort(pl, pl + n + 1);
    maxx = C * D2;
    int k = 0, t;
    do
    {
        t = Solve(k), k = t;
        if(t == -1) return 0;
    }while(t != INF);
    printf("%.2lf", ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值