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
不得不说自己再算法方面还是比较薄弱的,贪心算法听是听过但是从来没真正去编过==查了网上的方法,发现都是用贪心算法做的,哎==如果遇上了这种题,真是没办法了。以后这方面得加强了!还有一个惊喜是,这个代码正好一百行*_*
#include<iostream> #include<vector> #include<algorithm> using namespace std; struct Station { double price; double dis; Station(){ price = 0.0; dis = 0.0; } }; bool compareDistance(const Station &a,const Station &b) { return a.dis < b.dis; } int findNearestCheaperStation(int current,double remain,double unitDis,vector<Station> &s) { int cheaper = current; for (int i = current + 1; i < s.size() && (s[i].dis - s[current].dis <= unitDis*remain); ++i) { if (s[i].price < s[cheaper].price) { cheaper = i; break; } } return cheaper; } int findFarestStation(int current, double remain, double unitDis, vector<Station> &s) { int farest = current; for (int i = current+1; i < s.size() && (s[i].dis - s[current].dis <= unitDis*remain); ++i) { farest = i; } return farest; } int main() { double cap, distance, unitDis; int num,index; double money=0.0; double remainGas = 0.0; bool flag = true; scanf("%lf%lf%lf%d",&cap,&distance,&unitDis,&num); vector<Station> station(num); for (int i = 0; i < num; ++i) scanf("%lf%lf",&station[i].price,&station[i].dis); sort(&station[0],&station[0]+num,compareDistance); //加入终结点 Station aim; aim.dis = distance; station.push_back(aim); index = 0; for (int i = 0; i < num;) { //如果找到最近比当前站便宜并且能到达的,开车过去 index = findNearestCheaperStation(i,remainGas,unitDis,station); if (i != index) { remainGas -= (station[index].dis - station[i].dis) / unitDis; i = index; continue; } //加满油能找到的便宜的站并开车过去 index = findNearestCheaperStation(i, cap, unitDis, station); if (i != index) { money += ((station[index].dis - station[i].dis) / unitDis - remainGas)*station[i].price; remainGas = 0.0; i = index; continue; } //没有找到更便宜的则在当前站加满油开车到最远的站 index = findFarestStation(i, cap, unitDis, station); if (i!=index) { money += (cap - remainGas)*station[i].price; remainGas = cap - (station[index].dis - station[i].dis)/unitDis; i = index; continue; } else { flag = false; printf("The maximum travel distance = %0.2lf", station[i].dis + cap*unitDis); break; } } if (flag) { printf("%0.2lf", money); } return 0; }