标签:设置 turn inf ice names print while 没有 信息
#include<cstdio>
#include<algorithm>
using namespace std;
const int INF = 1000000000; //初始化最低油价
struct station{
double oil_price; //加油站油价
int distance; //加油站离出发点的距离
}sta[501];
bool cmp(station a, station b){
return a.distance < b.distance; //各个加油站,按照与出发点的距离从小到大排序
}
int main(){
double V, D, D_V;
int station_number;
scanf("%lf %lf %lf %d", &V, &D, &D_V, &station_number);
//把终点看作为一个加油站,则station + 1
for(int i = 0; i < station_number + 1; i++){
//如果,i为最后一个加油站,初始化最后一个加油站,即终点
if(i == station_number){
sta[i].oil_price = 0;
sta[i].distance = D;
}else{ //否则,输入剩余的加油站信息
scanf("%lf %d", &sta[i].oil_price, &sta[i].distance);
}
}
sort(sta, sta + station_number, cmp);
//排序后,如果没有距离与出发点为0的加油站,则无法出啊,距离为0;
if(sta[0].distance != 0){
printf("The maximum travel distance = 0.00");
}else{ //否则,能出发
int now = 0; //当前所处的加油站的编号
//总花费, 当前油量, 满油行驶的距离
double ans = 0, nowTank = 0, MAX = V * D_V;
while(now < station_number) {//每次循环选出下一个需要到达的加油站
//选出从当前加油站满油能到达范围内的第一个油价低于当前油价的加油站,
//如果没有低于当前油价的加油站,则选择油价最低的那个
int k = -1; //最低油价的加油站的编号
double priceMin = INF; //最低油价
for(int i = now + 1; i <= station_number && sta[i].distance - sta[now].distance <= MAX; i++) {
if(sta[i].oil_price < priceMin) { //如果油价比当前油价低
priceMin = sta[i].oil_price; //更新最低油价
k = i;
if(priceMin < sta[now].oil_price) {
//如果找到第一个油价低于当前油价的加油站,直接中断循环
break;
}
}
}
if(k == -1) break; //油箱已满的情况下,无法加油,即为不需找加油站,退出循环
//下面为计算能到达的加油站k,计算转移所用的油费
//need为:从now到k所需要的油量
double need = (sta[k].distance - sta[now].distance) / D_V;
if(priceMin < sta[now].oil_price) { //如果加油站k的油价低于当前油价
//只卖够到达加油站k的油
if(nowTank < need) { //如果当前油量不足need
ans += (need - nowTank) * sta[now].oil_price; //补足need
nowTank = 0; //到达加油站,油箱油量置为0,即为空
} else { //如果油量超过need
nowTank -= need; //直接到达加油站k
}
} else { //如果加油站k的油价高于当前的油价
ans += (V - nowTank) * sta[now].oil_price;
//到达加油站k后,把油箱内油量设置为 V - need
nowTank = V - need;
}
now = k; //到达加油站k,进入下一层循环
}
if(now == station_number) { //能到达终点
printf("%.2f\n", ans);
} else { //到不了终点
printf("The maximum travel distance = %.2f\n", sta[now].distance + MAX);
}
}
return 0;
}
标签:设置 turn inf ice names print while 没有 信息
原文地址:https://www.cnblogs.com/zjsaipplp/p/10425235.html