题目1437:To Fill or Not to Fill

题目1437:To Fill or Not to Fill

时间限制:1 秒

内存限制:128 兆

特殊判题:

提交:1040

解决:223

题目描述:

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、如果当前点加满也到不了下个点(包含终点),能走多远走多远,结束
3、寻找当前点A方圆内最便宜的,如果A即为最便宜的,则如果A可以到终点,则加到终点油量即可,出循环,如果
A不可到终点,则加满,去往下一个加油站;如果A方圆内B为最便宜的,则直接加油加到刚好可以到B,跳到B

c++代码:
//
#include
#include
#include
using namespace std;
#define INF 0x7FFFFFFF
#define ZERO 1e-8
//#define min(x,y) (x)<(y)?(x):(y)
const int MAX = 510;
struct node
{
	double pos;
	double price;
	bool operator < (const node &A)const
	{
		return pos>Cmax>>Dis>>Davg>>n)
	{
		for(int i=0; i 1e-8)//测试点 
		{
			//不能出发 
			printf("The maximum travel distance = %.2lf\n",currentDis);
			continue;//返回 
		}
		int i;
		bool flag = false;
		for(i=0; itank[i].pos+maxd)
					{
						break;
					}
					else if(tank[j].price currentGas)
					 	{
					 		cost += (need-currentGas)*tank[i].price;
					 	}
					 	flag = true;
					 	break;//到了 
					 }
				}
				else
				{
					double tp = tank[minp].pos-tank[i].pos;
					double need = tp/Davg;
					if(need > currentGas)
					{
						cost += (need-currentGas)*tank[i].price;
						currentGas = 0;
					}
					else
						currentGas -= need;
					i = minp-1;
				}
			}
		}
		if(flag==true)
		//打印cost 
			printf("%.2lf\n",cost);
	
	}
}


你可能感兴趣的:(九度ACM,九度解题报告)