九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题

题目1437:To Fill or Not to Fill

时间限制:1 秒

内存限制:128 兆

特殊判题:

提交:1488

解决:345

题目描述:

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.

输入:

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.

输出:

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.

样例输入:
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
50 1300 12 2
7.10 0
7.00 600
样例输出:
749.17
The maximum travel distance = 1200.00
来源:
2012年浙江大学计算机及软件工程研究生机试真题
分析:
  1 //贪心算法
  2 //dis=满油箱可以开出的最远距离。
  3 //算法描述:起点A开始,到A+dis范围内:
  4 //1.如果存在点B的s[B].price<=s[A].price,只要满足能行驶到B点即可
  5 //2.如果不存在点B的s[B].price<=s[A].price,则要使车能开到 A+dis范围内除A以外,price最小的点--min_index所指点 
  6 //注意:
  7 //1.终点如果在 A+dis范围内,一定可达
  8 //2.注意排序后第一个点的dis可能不为0
  9 //3.离A点最近的B点与A的距离如果>dis,则此趟行驶到不了终点 
 10 //4.油箱的情况每次都要关注
 11 //具体见代码: 
 12 #include<iostream>
 13 #include<queue>
 14 #include<cstdio>
 15 #include<cstring>
 16 #include<cmath>
 17 #include<algorithm>
 18 using namespace std;
 19 struct station{ 
 20     double price,dis;
 21 }; 
 22 station s[505];
 23 bool cmp(station a,station b){
 24     return a.dis<b.dis;
 25 }
 26 int main(){
 27     double cmax,d,davg;
 28     int n;
 29     while(scanf("%lf%lf%lf%d",&cmax,&d,&davg,&n)!=EOF){
 30         double min_price=0;
 31         double max_dis=0;
 32         double cur_tank=0;
 33         int i=0;
 34         for(;i<n;i++){
 35             cin>>s[i].price>>s[i].dis;
 36         }
 37         s[i].dis=d;
 38         s[i].price=0;
 39         sort(s,s+n,cmp);
 40         /*for(i=0;i<=n;i++){
 41             cout<<s[i].dis<<' '<<s[i].price<<endl;
 42         }*/
 43         if(s[0].dis>0){//2.注意排序后第一个点的dis可能不为0
 44             //cout<<1<<endl;
 45             printf("The maximum travel distance = 0.00\n");
 46             continue;
 47         }
 48         int f=0;
 49         double dis=cmax*davg;
 50         int min_index;
 51         while(s[f].dis<d){
 52             int next=f+1;
 53             min_index=next;
 54             while((s[next].dis-s[f].dis)<=dis){//如果next==n,一定会从break处出去
 55             //跳出循环只有三种情况: 
 56             //1.在dis范围内,没有点与f点的距离小于等于dis 
 57             //2.在dis范围内,找到price小于f的点(包括next==n) 
 58             //3.next!=n,但在dis范围内,没有找到price小于f的点。在dis范围内,有点与f点的距离小于等于dis 
 59                 if(s[next].price<s[min_index].price){//记录最小的油价的站点 
 60                     min_index=next;
 61                 }
 62                 if(s[next].price<=s[f].price){//在dis范围内,找到price小于f的点  
 63                     break;
 64                 }
 65                 next++;
 66             }
 67             if(next==f+1&&(s[next].dis-s[f].dis)>dis){//1.在dis范围内,没有点与f点的距离小于等于dis 
 68                 //cout<<1<<endl;
 69                 max_dis+=dis;
 70                 break;
 71             }
 72             else{
 73                 if((s[next].dis-s[f].dis)<=dis){//2.在dis范围内,找到price小于f的点(包括next==n) 
 74                     if(s[next].dis-s[f].dis>cur_tank){
 75                         min_price+=(s[next].dis-s[f].dis-cur_tank)*s[f].price;
 76                         cur_tank=0;
 77                         max_dis+=s[next].dis-s[f].dis;
 78                     }
 79                     else{
 80                         cur_tank-=s[next].dis-s[f].dis;
 81                         max_dis+=s[next].dis-s[f].dis;
 82                     }
 83                     f=next;
 84                 }
 85                 else{//3.next!=n,但在dis范围内,没有找到price小于f的点。在dis范围内,有点与f点的距离小于等于dis
 86                     min_price+=(dis-cur_tank)*s[f].price;
 87                     cur_tank=dis-(s[min_index].dis-s[f].dis);
 88                     max_dis+=s[min_index].dis-s[f].dis;
 89                     f=min_index;
 90                 }
 91             }
 92         }
 93         if(s[f].dis==d){
 94             printf("%.2lf\n",min_price/davg);
 95         } 
 96         else{
 97             printf("The maximum travel distance = %.2lf\n",max_dis*1.0);//注意输出格式 
 98         }
 99     }
100     return 0;
101 }

 

 
posted @ 2015-02-13 18:39  Deribs4  阅读(1031)  评论(0编辑  收藏  举报