1033. 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.
Input Specification:
Each input file contains one test case. For each case, the first line contains 4 positive numbers: Cmax (<= 100), the maximum capacity of the tank; D (<=30000), the distance between Hangzhou and the destination city; Davg (<=20), the average distance per unit gas that the car can run; and N (<= 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: Pi, the unit gas price, and Di (<=D), the distance between this station and Hangzhou, for i=1,...N. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print the cheapest price in a line, accurate up to 2 decimal places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, print "The maximum travel distance = X" where X is the maximum possible distance the car can run, accurate up to 2 decimal places.
Sample Input 1:
50 1300 12 8 6.00 1250 7.00 600 7.00 150 7.10 0 7.20 200 7.50 400 7.30 1000 6.85 300
Sample Output 1:
749.17
Sample Input 2:
50 1300 12 2 7.10 0 7.00 600
Sample Output 2:
The maximum travel distance = 1200.00
#include<cstdio> #include<algorithm> using namespace std; const int INF = 1000000000; struct gas{ double dis,price; }st[510]; bool cmp(gas a, gas b){ return a.dis < b.dis; } int main(){ int N; double Cmax,D,Davg;//油箱容量,城市距离,每升汽油跑的距离,加油站数量 scanf("%lf%lf%lf%d",&Cmax,&D,&Davg,&N); for(int i = 0; i < N; i++){ scanf("%lf%lf",&st[i].price,&st[i].dis); } st[N].price = 0; st[N].dis = D; sort(st,st+N,cmp); if(st[0].dis != 0){ printf("The maximum travel distance = 0.00\n"); }else{ int now = 0; double ans = 0, nowTank = 0, maxDis = Cmax * Davg;//总花费,当前油箱油量,满油箱跑的距离 while(now < N){ int k = -1; //最低油价加油站编号 double minPrice = INF; for(int i = now+1; i <= N && st[i].dis - st[now].dis <= maxDis;i++){ if(st[i].price < minPrice){ minPrice = st[i].price; k = i; } if(minPrice < st[now].price){ break; } } if(k == -1) break; double need = (st[k].dis - st[now].dis) /Davg; // 开过去所需的油量 if(minPrice < st[now].price){ //当前加油站的油价高 if(nowTank < need){ //油箱的油不够到达地点 ans += st[now].price * (need - nowTank); nowTank = 0;//到达地点后油箱空 }else{ nowTank -= need; } } else{ //当前加油站油价低 ans += (Cmax - nowTank) * st[now].price; nowTank = Cmax - need; } now = k; } if(now == N) printf("%.2f\n",ans); else printf("The maximum travel distance = %.2f\n",st[now].dis+maxDis); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)