题目25:To Fill or Not to Fill

http://ac.jobdu.com/problem.php?cid=1040&pid=24

题目描述:

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
// 题目25:To Fill or Not to Fill.cpp: 主项目文件。

#include "stdafx.h"
#include <cstdio>
#include <algorithm>
using std::sort;

const int N=503;
typedef struct Station
{
	double avgPrice;
	double dd;
}Station;
Station sta[N];
int n;

bool cmp(Station m1, Station m2)
{
	return m1.dd<m2.dd;
}

double isReachable(double dist)
{
	for(int i=1;i<n;i++)
	{
		if(sta[i].dd-sta[i-1].dd>dist)
			return sta[i-1].dd+dist;
	}
	return sta[n-1].dd+dist;
}

double computeMinCost(double desDist, double cmax, double dvag)
{
	double dist=cmax*dvag,res=0,oil=0;
	int count=0,i=0;
	while(sta[count].dd<desDist)
		count++;
	count-=2;
	while(i<=count){
		if(sta[i].avgPrice>=sta[i+1].avgPrice){
			if(oil>=(sta[i+1].dd-sta[i].dd)/dvag)
				oil-=(sta[i+1].dd-sta[i].dd)/dvag;
			else{
				res+=sta[i].avgPrice*((sta[i+1].dd-sta[i].dd)/dvag-oil);
				oil=0;
			}
			i++;
		}
		else{ 
			int t=i+1,minf=1000;
			double min=desDist+1;
			bool tag=false;
			while(t<=count+1&&sta[t].dd-sta[i].dd<=dist){
				if(sta[t].avgPrice<=sta[i].avgPrice){
					tag=true;
					break;
				}
				if(sta[t].avgPrice<min){
					min=sta[t].avgPrice,minf=t;
				}
				t++;
			}
			if(tag){
				if(oil>=(sta[t].dd-sta[i].dd)/dvag)
					oil-=(sta[t].dd-sta[i].dd)/dvag;
				else{	
					res+=sta[i].avgPrice*((sta[t].dd-sta[i].dd)/dvag-oil);
					oil=0;
				}
				i=t;
			}
			else{
				//t=minf;
				if(desDist-sta[i].dd>dist){
					res+=sta[i].avgPrice*(cmax-oil);//((sta[t].dd-sta[i].dd)/dvag-oil);
					oil=cmax-(sta[minf].dd-sta[i].dd)/dvag;i=minf;
				}
				else
				//if(t==count+2)
					return res+sta[i].avgPrice*((desDist-sta[i].dd)/dvag-oil);
			}
		}
	}
	if(oil>=(desDist-sta[i].dd)/dvag)
		return res;
	else
		return res+sta[i].avgPrice*((desDist-sta[i].dd)/dvag-oil);
}

int main()
{
	//freopen("F:\\test.txt","r",stdin);
	//freopen("F:\\output.txt","w",stdout);
	double cmax,dist,dvag;
	while(~scanf("%lf%lf%lf%d",&cmax,&dist,&dvag,&n))
	{
		for(int i=0;i<n;i++)
			scanf("%lf%lf",&sta[i].avgPrice,&sta[i].dd);
		sta[n].dd=dist+1;
		sort(sta,sta+n,cmp);
		if(sta[0].dd!=0)
		{
			printf("The maximum travel distance = 0.00\n");
			continue;
		}
		double far=isReachable(cmax*dvag);
		if(far>=dist)
		{
			double res=computeMinCost(dist, cmax, dvag);
			printf("%.2lf\n",res);
		}
		else
			printf("The maximum travel distance = %.2lf\n",far);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值