pat1033. To Fill or Not to Fill (25)

1033. To Fill or Not to Fill (25)

时间限制

10 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

ZHANG, Guochuan

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


提交代码

  1 #include<cstdio>
  2 #include<stack>
  3 #include<algorithm>
  4 #include<iostream>
  5 #include<stack>
  6 #include<set>
  7 #include<map>
  8 using namespace std;
  9 struct station
 10 {
 11     double price,dis;
 12 };
 13 station sta[505];
 14 bool cmp(station a,station b)
 15 {
 16     if(a.dis==b.dis)
 17     {
 18         return a.price<b.price;
 19     }
 20     return a.dis<b.dis;
 21 }
 22 //找到第一个最近的油价比当前加油站少的加油站A,在当前的加油站,只要加满到A的油
 23 //如果没有找到,看一下当前能否到达目的地,如果可以,只要加到目的地的油;
 24 //如果不能,找在范围内的油价最低的加油站,在当前的加油站加满油,到下一个加油站
 25 //如果在范围内没有找到加油站,说明不能到达目的地,输出最远距离=当前加油站的位置+满油的距离
 26 //除了标号,其他都为double值!!
 27 int main()
 28 {
 29     //freopen("D:\\INPUT.txt","r",stdin);
 30     int N;
 31     double D,Davg,cmax;
 32     scanf("%lf %lf %lf %d",&cmax,&D,&Davg,&N);
 33     int i;
 34     for(i=0; i<N; i++)
 35     {
 36         scanf("%lf %lf",&sta[i].price,&sta[i].dis);
 37     }
 38     sta[i].dis=D;
 39     sta[i].price=-1;
 40     sort(sta,sta+N+1,cmp);
 41     double fardis=cmax*Davg;
 42     if(sta[0].dis!=0)//第一个加油站不在目的地
 43     {
 44         printf("The maximum travel distance = 0.00\n");
 45         return 0;
 46     }
 47     if(D==0)//第一个加油站就是目的地
 48     {
 49         printf("0.00\n");
 50         return 0;
 51     }
 52     int cursta=0,nextsta;
 53     double tank=0;
 54     double totalmon=0;
 55     while(sta[cursta].price!=-1)//没有到目的地
 56     {
 57         double minprice=sta[cursta].price;
 58         int minsta=-1;
 59         for(nextsta=cursta+1; nextsta<=N&&sta[cursta].dis+fardis>=sta[nextsta].dis; nextsta++)
 60             //找距离最近的油费最低的点
 61         {
 62             if(sta[nextsta].price<=minprice)
 63             {
 64                 minsta=nextsta;
 65                 break;
 66             }
 67         }//找到就出手
 68         if(minsta!=-1) //找到了,有可能是目的地,有可能是加油站
 69         {
 70             double t=(sta[minsta].dis-sta[cursta].dis)/Davg;
 71             //cout<<t<<endl;
 72             if(tank<t)
 73             {
 74                 totalmon+=sta[cursta].price*(t-tank);
 75                 tank=0;
 76             }
 77             else
 78             {
 79                 tank-=t;
 80             }
 81             cursta=minsta;
 82         }
 83         else
 84         {//目的地不在当前的可达范围内,可达范围内要么都是油价较高的加油站,要么没有加油站
 85             for(nextsta=cursta+1; nextsta<=N&&sta[cursta].dis+fardis>=sta[nextsta].dis; nextsta++)
 86             {
 87                 if(sta[nextsta].price>minprice)
 88                 {
 89                     minprice=sta[nextsta].price;
 90                     minsta=nextsta;
 91                     break;
 92                 }
 93             }
 94             for(nextsta++; nextsta<=N&&sta[cursta].dis+fardis>=sta[nextsta].dis; nextsta++)
 95             {
 96                 if(sta[nextsta].price>sta[cursta].price&&sta[nextsta].price<minprice)
 97                 {
 98                     minprice=sta[nextsta].price;
 99                     minsta=nextsta;
100                 }
101             }
102             if(minsta!=-1) //有加油站
103             {
104                 totalmon+=(cmax-tank)*sta[cursta].price;
105                 tank=cmax-(sta[minsta].dis-sta[cursta].dis)/Davg;
106                 cursta=minsta;
107             }
108             else //不可达
109             {
110                 printf("The maximum travel distance = %.2lf\n",sta[cursta].dis+fardis);
111                 return 0;
112             }
113         }
114     }
115     printf("%.2lf\n",totalmon);
116     return 0;
117 }
时间: 2024-10-05 11:29:06

pat1033. To Fill or Not to Fill (25)的相关文章

PAT 1033. To Fill or Not to Fill (25)(贪心)

解题思路: 其实题目本身不难,难就难在对贪心策略的选取 在每一个加油点应该做这样的判断: 1.首先计算从本加油点做多可能达到的加油点 2.如果有可达加油点的话: 2.1 寻找有无比当前加油点油价便宜的加油点,如果有的话就跑到该便宜的加油点,油量加到支持到该加油点即可 2.2 如果没有比当前加油点更便宜的加油点的话,又分两种情况: 2.2.1.如果本次加油可能直接到终点,就加可以到达终点的油量 2.2.2.否则的话,加满 3.如果没有可达加油点的话: 3.1 看是否可以直接达到终点 3.2 不能达

【贪心】PAT 1033. To Fill or Not to Fill (25)

1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 ZHANG, Guochuan 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 fin

1033 To Fill or Not to Fill (25 分)

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 g

九度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 fro

PAT 1033 To Fill or Not to Fill[dp]

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 gi

PAT1033.To Fill or Not to Fill

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

PAT 1004 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

PAT (Advanced Level) 1033. To Fill or Not to Fill (25)

贪心.注意x=0处没有加油站的情况. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; struct X { double cost, x, v; int

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