标签:
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 300Sample Output 1:
749.17Sample Input 2:
50 1300 12 2 7.10 0 7.00 600Sample 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 }
pat1033. To Fill or Not to Fill (25)
标签:
原文地址:http://www.cnblogs.com/Deribs4/p/4788083.html