PAT1033

To Fill or Not to Fill(25)

With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go.

#include<bits/stdc++.h>
using namespace std;
const int inf=1e9;
struct Node{
    float p;//price
    float d;//distance
};
bool cmp(Node a,Node b){return a.d<b.d;}
int main(){
    float c,d,a,n,w=0,left=0;
    int i,j;
    float sum=0.0;
    cin>>c>>d>>a>>n;
    vector<Node> v(n);
    for(i=0;i<n;++i)cin>>v[i].p>>v[i].d;
    sort(v.begin(),v.end(),cmp);
    i=1;
    if(v[0].d>0){printf("The maximum travel distance = %.2f",0);return 0;}
    Node x={v[0].p,0};
    while(w<d){
        Node mini={inf,0};
        if(d-w<=c*a)mini={x.p,d};//first price<last price
        while(i<v.size()&&v[i].d-w<=c*a){
            if(v[i].p<x.p){mini.p=v[i].p;mini.d=v[i].d;j=i;break;}
            else{//find the minimum price
                mini.p=min(v[i].p,mini.p);
                if(v[i].p==mini.p){mini.d=w+c*a;j=i;}
                ++i;
            }
        }
        if(mini.d==0){printf("The maximum travel distance = %.2f",w+c*a);break;}
        else {
                sum+=x.p*(mini.d-w-left)/a;
                //printf("\npass%f %d,add%f*%d",mini.p,v[j].d,x.p,(mini.d-w-left));
                if(mini.d==d){printf("%.2f",sum);break;}
                left=mini.d-v[j].d;
                w=v[j].d;i=j+1;x=mini;
            }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值