1033 To Fill or Not to Fill (25 point(s))
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
题意:
输入:第一行分别输入,油箱的容量(cap),目的地离杭州多远(des),车每升油能走多少公里(per),路上有多少个加油站(n),后面n行的就是n个加油站的油价和离杭州(起点)多元远
输出:如果能到达终点输出最低消费,否则就输出最多能走多远(输入保留两位小数);
思路:
很有意思的一题贪心算法题,每一步达到最优,从而局部最优,假设我们想要油费最小,我们在每个站就要思考好,可大范围内(cap*per)有没有比我所在站更便宜的油,如果有,我们是不是只需要加够到那一站的油就可以了,多加一点油都是浪费,如果没有呢,哪现在我们所在的站的油就是最便宜的油,我们应该在这一站加满,然后去到可达范围内油费最低的一站(但比原来哪站贵),再从该站的可大范围进行决策,最后得到的费用就是最低的。
代码:
#include<iostream>
#include<algorithm>
#include<vector>
#include<climits>
using namespace std;
struct station{
double p;//每个站的价格和离起点多远;
double d;
};
bool cmp(station a,station b){
return a.d<b.d;
}
int main(){
double cap,des,per,n;
cin>>cap>>des>>per>>n;
vector<station> s(n+1);
for(int i=0;i<n;i++){
cin>>s[i].p>>s[i].d;
}
s[n].d=des;s[n].p=0;
sort(s.begin(),s.end(),cmp);
if(s[0].d!=0){//起点必须有加油站,题目说了,开始车没有油;
cout<<"The maximum travel distance = 0.00";
return 0;
}
double range=per*cap;//可大范围;
double gasleft=0.00;//剩余多少油;
double cost=0.00;
double now=0.00;//现在在哪一个加油站;
bool unreachable=false;
while(now<n){
double minp=INT_MAX;//这里要注意,原来我是整型定义,然后拿这个整型的minp和s[i].d(double)比,会把浮点型强制转换成整型,带来误差,导致后续决策错误;
int next=-1;
bool foundcheap=false;
for (int i=now+1; s[i].d<=s[now].d+range; i++){
if(s[i].p<minp){//如果找不到比当前还便宜的,就找后面可达范围内种最便宜的;
minp=s[i].p;
next=i;
}
if(s[now].p>s[i].p){
next=i;
foundcheap=true;
break;
}
}
if(next==-1){//不在可大范围内,就是没有站可以去;
unreachable=true;
break;
}
if(foundcheap){
cost+=(((s[next].d-s[now].d)/per-gasleft)*s[now].p);//找到比原来站还便宜的就去哪里;
gasleft=0.00;//加够能去到的油就好了;
}else {
cost += ((cap - gasleft) * s[now].p);//此时所在站的油才是最便宜的情况,在原来那站加满油再出发;
gasleft = (cap - (s[next].d - s[now].d) / per);
}
now=next;//去到下一站,更新所在站位;
}
if(unreachable){
printf("The maximum travel distance = %.2lf",s[now].d+range);
}else {
printf("%.2lf",cost);
}
return 0;
}
纯净版:
#include<iostream>
#include<vector>
#include<algorithm>
#include<climits>
using namespace std;
struct station{
double p;
double d;
};
bool cmp(station a,station b){
return a.d<b.d;
}
int main(){
double cap,des,per,n;
cin>>cap>>des>>per>>n;
vector<station> s(n+1);
for(int i=0;i<n;i++){
cin>>s[i].p>>s[i].d;
}
s[n].p=0;s[n].d=des;
sort(s.begin(),s.end(),cmp);
if(s[0].d!=0){
cout<<"The maximum travel distance = 0.00";
return 0;
}
double now=0.00;
double range=cap*per;
double cost=0.00;
double gasleft=0.00;
bool unreachable=false;
while(now<n){
bool foundcheap=false;
double minp=INT_MAX;
int next=-1;
for(int i=now+1;s[i].d<=s[now].d+range;i++){
if(s[i].p<minp){
next=i;
minp=s[i].p;
}
if(s[i].p<s[now].p){
next=i;
foundcheap=true;
break;
}
}
if(next==-1){
unreachable=true;
break;
}
if(foundcheap){
cost+=((s[next].d-s[now].d)/per-gasleft)*s[now].p;
gasleft=0.00;
}else {
cost+=(cap-gasleft)*s[now].p;
gasleft=(cap-(s[next].d-s[now].d)/per);
}
now=next;
}
if(unreachable){
printf("The maximum travel distance = %.2lf",s[now].d+range);
}else {
printf("%.2lf",cost);
}
return 0;
}
注意:如果测试点2没过的话,考虑到不了目的地情况的输出,比方说起点没有加油站,或有加油站到不了终点,如样例2;