题目描述:

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
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef struct
{
double p;
double d;
}station;
bool compare(station s1,station s2)
{
return s1.d<s2.d;
}
int main()
{
int n,i,j,k;
double cmax,d,davg,c,min,price,dis;
while(cin>>cmax>>d>>davg>>n)
{
station s[501];
for(i=0;i<n;i++)
cin>>s[i].p>>s[i].d;
sort(s,s+n,compare);
s[n].d=d;
s[n].p=0;
if(s[0].d!=0)
{
cout<<"The maximum travel distance = 0.00"<<endl;
continue;
}
c=cmax;//油箱剩余容量
price=dis=0;
for(i=0;i<n;)
{
if(s[i+1].d-s[i].d>cmax*davg)
{
dis+=cmax*davg;
break;
}
k=-1;
for(j=i+1;j<n&&(s[j].d-s[i].d)<=davg*cmax;j++)//找能到达的比现在的便宜的最近的加油站
if(s[j].p<s[i].p)
{
k=j;
break;
}
if(k==-1)//能到的都比现在的贵
{
if(cmax*davg>=(d-s[i].d))//现在的装满油能到终点站
{
dis=d;
price+=(d-s[i].d-(cmax-c)*davg)/davg*s[i].p;
break;
}
else//找一个能到达的最便宜的
{
min=s[i+1].p;
k=i+1;
for(j=i+2;j<n&&(s[j].d-s[i].d)<=davg*cmax;j++)
{
if(s[j].p<min)
{
min=s[j].p;
k=j;
}
}
dis=s[k].d;
price+=c*s[i].p;
c=(s[k].d-s[i].d)/davg;
i=k;
}
}
else
{
dis=s[k].d;
price+=(s[k].d-s[i].d-(cmax-c)*davg)/davg*s[i].p;
c=cmax;
i=k;
}
}
if(dis==d)
printf("%.2lf\n",price);
else
printf("The maximum travel distance = %.2lf\n",dis);
}
return 0;
}

  

九度 题目1437:To Fill or Not to Fill的更多相关文章

  1. 九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题

    题目1437:To Fill or Not to Fill 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1488 解决:345 题目描述: With highways availabl ...

  2. 九度OJ 1437 To Fill or Not to Fill -- 贪心算法

    题目地址:http://ac.jobdu.com/problem.php?pid=1437 题目描述: With highways available, driving a car from Hang ...

  3. 九度OJ #1437 To Fill or Not to Fil

    题目描写叙述: With highways available, driving a car from Hangzhou to any other city is easy. But since th ...

  4. 九度OJ 1437 To Fill or Not to Fill

    题目大意:小明从杭州去往某目的地,要经过一些加油站,每个加油站的价格不一样.若能顺利到达,求加油费用最少为多少,否则求出能行驶的最远距离. 思路:贪心算法 1>若下一加油站的价格更便宜,则只需走 ...

  5. 九度 题目1044:Pre-Post

    转载请注明本文链接http://blog.csdn.net/yangnanhai93/article/details/40658571 题目链接:pid=1044">http://ac ...

  6. 九度 题目1421:Abor

    转载声明本文地址 http://blog.csdn.net/yangnanhai93/article/details/40563285 题目链接:http://ac.jobdu.com/problem ...

  7. 九度-题目1203:IP地址

    http://ac.jobdu.com/problem.php?pid=1203 题目描述: 输入一个ip地址串,判断是否合法. 输入: 输入的第一行包括一个整数n(1<=n<=500), ...

  8. 九度-题目1026:又一版 A+B

    http://ac.jobdu.com/problem.php?pid=1026 题目描述: 输入两个不超过整型定义的非负10进制整数A和B(<=231-1),输出A+B的m (1 < m ...

  9. 九度-题目1195:最长&最短文本

    http://ac.jobdu.com/problem.php?pid=1195 题目描述: 输入多行字符串,请按照原文本中的顺序输出其中最短和最长的字符串,如果最短和最长的字符串不止一个,请全部输出 ...

随机推荐

  1. [Java解惑]异常

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  2. C#强制清除缓存

    #region 强制清除缓存 [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize" ...

  3. iOS : 静态库(.framework)合并

    如果写了一个Framework,根据Build时选择的机器类型,会分为模拟器Framework和真机Framework,两者是不能混用的. 此时可以通过配置一个Run Script,在Script中使 ...

  4. C# 线程(二):关于线程的相关概念

    From : http://kb.cnblogs.com/page/42528/ 什么是进程? 当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源. 而一个进程又 ...

  5. elcipse 中利用maven创建web工程

    如何创建: http://huxiaoheihei.iteye.com/blog/1766986 遇到的问题: 1: 如果spring MVC配置了 <servlet> <servl ...

  6. iOS - Alamofire 网络请求

    前言 Alamofire 是 Swift 语言的 HTTP 网络开发工具包,相当于 Swift 实现 AFNetworking 版本.当然,AFNetworking 非常稳定,在 Mac OSX 与 ...

  7. 使用BAPI_ACC_DOCUMENT_POST,创建会计凭证,用BADI扩展字段(转)

    业务需求:和银行做一个接口,要通过银行流水产生会计凭证,会计凭证的事务码是F-02,查到了BAPI方法BAPI_ACC_DOCUMENT_POST.昨天测试发现,有一些参数在BAPI_ACC_DOCU ...

  8. [转载] 构建微服务:使用API Gateway

    原文: http://mp.weixin.qq.com/s?__biz=MzA5OTAyNzQ2OA==&mid=206889381&idx=1&sn=478ccb35294c ...

  9. 线程高级应用-心得6-java5线程并发库中同步工具类(synchronizers),新知识大用途

    1.新知识普及 2. Semaphore工具类的使用案例 package com.java5.thread.newSkill; import java.util.concurrent.Executor ...

  10. MyEclipse8.5启动无法选择工作空间的问题

    方法一:打开Window---Preferences---General---Startup and Shutdown,勾选Prompt for workspace on startup 选项,再次登 ...