PAT 1033. To Fill or Not to Fill

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

分析:贪心算法。(转载)
0.假设增加一个目的地处的加油站,距离为目的地的距离,价格为0,考虑从0距离开始能否到达最后一个加油站的问题
1.因为先开始没有油,所以如果所有的加油站距离都没有等于0的,那么说明车哪也去不了,直接输出并return
2.将加油站按照距离dis从小到大排序
3.先去第一个加油站,设置变量nowdis表示当前所在的距离,maxdis是能够到达的最大距离,nowprice是当前的站点的价格,totalPrice是总的价格。
贪心思想:
0.寻找比自己距离远的,到能够到达的最大距离之间的加油站,看他们的油价。如果找到了更低价格的油价,就加油到刚好能到达那个加油站的距离的油,然后去那个更低价格的加油站(有更低的我一分都不想多花在别的距离上,只加到刚好满足更低价格的加油站的距离就行,那样以后的路程我就可以以更低的价格行驶啦)

1.如果找不到更低的,就找尽可能低的油价的加油站,在当前加油站加满油之后过去。因为想要让路程上使用的尽可能是低价的油,既然没有比当前更低价格的了,就让油箱加到最大值,这样能保证利益最大化,保证最大的距离使用的是便宜的油。



自己的代码
 1 #include<iostream>
 2 #include<vector>
 3 #include<algorithm>
 4 using namespace std;
 5 struct st{ //加气站节点
 6     double price,dist;
 7 }; 
 8 bool cmp(const st& t1,const st& t2){
 9     return t1.dist<t2.dist;
10 }
11 int main(){
12      /*tag指向出发站,s是行驶的路程,cheaper来记录行驶过程中最便宜的气,
13      cost记录花费,leftdist记录剩余的气还可以走多远,flag是下面的标记*/
14     double cmax,d,davg,tag=0,s=0,cheaper=99999,cost=0,leftdist=0,flag;
15     int n;
16     cin>>cmax>>d>>davg>>n;
17     vector<st> sta(n+1);
18     for(int i=0;i<n;i++)
19         cin>>sta[i].price>>sta[i].dist;
20     sta[n].dist=d; sta[n].price=0;
21     sort(sta.begin(),sta.end(),cmp);
22     if(sta[0].dist!=0){ //起点处没有加气站
23         printf("The maximum travel distance = 0.00");
24         return 0;
25     }
26     while(s<d){ 
27     /*如果出发处的下一站距离出发处距离大于加满气时能跑的距离,到达不了终点*/
28         if(sta[tag+1].dist-s>cmax*davg){
29            printf("The maximum travel distance = %.2lf",s+cmax*davg);
30            return 0;
31         }
32         for(int i=tag+1;i<=n&&(sta[i].dist-s)<=cmax*davg;i++){
33              /*如果在出发后遇到比出发时价格更低的加气站,就只加到刚好到这个站的气就好了*/
34             if(sta[i].price<sta[tag].price){
35                cost+=(sta[i].dist-s-leftdist)/davg*sta[tag].price;
36                tag=i; s=sta[i].dist; leftdist=0.0;
37                flag=-1; 
38                break;
39             }
40              /*如果没有价格更低的加气站,就在范围内找寻最低的加气站,并在tag指向的加气站把气加满*/
41             else if(sta[i].price<cheaper){
42                 flag=i; cheaper=sta[i].price;
43             }
44         }
45         /*如果是没有价格更低的加气站时的处理*/
46         if(flag!=-1){
47            cost+=(cmax-leftdist/davg)*sta[tag].price;
48            leftdist=cmax*davg-(sta[flag].dist-sta[tag].dist); 
49            s=sta[flag].dist; tag=flag; cheaper=99999;
50         }
51     }
52     printf("%.2lf",cost);
53 }

别人的代码
 1 #include <cstdio>  
 2 #include <algorithm>  
 3 #include <vector>  
 4 using namespace std;  
 5 const int inf = 99999999;  
 6 struct station {  
 7     double price, dis;  
 8 };  
 9 bool cmp1(station a, station b) {  
10     return a.dis < b.dis;  
11 }  
12 int main() {  
13     double cmax, d, davg;  
14     int n;  
15     scanf("%lf%lf%lf%d", &cmax, &d, &davg, &n);  
16     vector<station> sta(n + 1);  
17     sta[0].price = 0.0;  
18     sta[0].dis = d;  
19     for(int i = 1; i <= n; i++) {  
20         scanf("%lf%lf", &sta[i].price, &sta[i].dis);  
21     }  
22     sort(sta.begin(), sta.end(), cmp1);  
23     double nowdis = 0.0,  maxdis = 0.0, nowprice = 0.0, totalPrice = 0.0, leftdis = 0.0;  
24     if(sta[0].dis != 0) {  
25         printf("The maximum travel distance = 0.00");  
26         return 0;  
27     } else {  
28         nowprice = sta[0].price;  
29     }  
30     while(nowdis < d) {  
31         maxdis = nowdis + cmax * davg;  
32         double minPriceDis = 0, minPrice = inf;  
33         int flag = 0;  
34         for(int i = 1; i <= n && sta[i].dis <= maxdis; i++) {  
35             if(sta[i].dis <= nowdis) continue;  
36             if(sta[i].price < nowprice) {  
37                 totalPrice += (sta[i].dis - nowdis - leftdis) * nowprice / davg;  
38                 leftdis = 0.0;  
39                 nowprice = sta[i].price;  
40                 nowdis = sta[i].dis;  
41                 flag = 1;  
42                 break;  
43             }  
44             if(sta[i].price < minPrice) {  
45                 minPrice = sta[i].price;  
46                 minPriceDis = sta[i].dis;  
47             }  
48         }  
49         if(flag == 0 && minPrice != inf) {  
50             totalPrice += (nowprice * (cmax - leftdis / davg));  
51             leftdis = cmax * davg - (minPriceDis - nowdis);  
52             nowprice = minPrice;  
53             nowdis = minPriceDis;  
54               
55         }  
56         if(flag == 0 && minPrice == inf) {  
57             nowdis += cmax * davg;  
58             printf("The maximum travel distance = %.2f", nowdis);  
59             return 0;  
60         }  
61     }  
62     printf("%.2f", totalPrice);  
63     return 0;  
64 }  
原文地址:https://www.cnblogs.com/A-Little-Nut/p/8296985.html