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 maxn = 510;
const int INF = 0x3f3f3f3f;
typedef struct {
double dis;
double price;
}Station;
Station sta[maxn];
bool cmp(Station a, Station b);
int main(int argc, char** argv) {
double Cmax, D, Davg;
int N;
scanf("%lf %lf %lf %d", &Cmax, &D, &Davg, &N);
for(int i = 0; i < N; i++){
scanf("%lf %lf", &sta[i].price, &sta[i].dis);
}
sta[N].dis = D;
sta[N].price = 0;
sort(sta, sta + N + 1, cmp);
int now = 0;
double nowDis = 0, totalPrice = 0, nowGas = 0;
bool flag = true;
if(sta[now].dis > 0){ //特殊情况判断
printf("The maximum travel distance = 0.00");
return 0;
}
while(now < N){
int target = -1;
double minPrice = INF;
for(int i = now + 1; i <= N && sta[i].dis <= nowDis + Cmax * Davg; i++){ //寻找能开到的最大范围内第一个价格比当前价格低的车站
if(sta[i].price <= minPrice){
target = i;
minPrice = sta[i].price;
if(minPrice < sta[now].price){ //如果找到比当前油费便宜的车站
break;
}
}
}
if(target == -1){ //油加满也到不了下一个车站
flag = false;
break;
}
double needGas = (sta[target].dis - nowDis) / Davg; //当前车站到下一车站需要多少油
if(sta[now].price < minPrice){ //如果最便宜的车站是当前车站,那就把油加满
totalPrice += sta[now].price * (Cmax - nowGas);
nowGas = Cmax - needGas;
now = target;
nowDis = sta[target].dis;
}
else{ //把车开到最便宜的的车站处
if(nowDis + nowGas * Davg < sta[target].dis){ //现在的油不能开到最低价格的车站处
totalPrice += sta[now].price * (needGas - nowGas);
nowGas = 0;
}else{ //现在的油足够开到下一个比当前车站便宜的车站处
nowGas -= needGas;
}
now = target;
nowDis = sta[target].dis;
}
}
if(flag == false){
printf("The maximum travel distance = %.2f", nowDis + Cmax * Davg);
}else{
printf("%.2f", totalPrice);
}
return 0;
}
bool cmp(Station a, Station b){
return a.dis < b.dis;
}
本文介绍了一种算法,用于计算从杭州到其他城市之间的最经济驾驶路线。考虑到油箱容量限制和沿途不同油价的加油站,该算法通过贪心策略确定最佳加油点,以实现总油费最小化。
1359

被折叠的 条评论
为什么被折叠?



