题目描述:

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. [转]Jenkins CommonCollections 完美利用(演示)工具

    博主URL:http://tools.changesec.com/Jenkins-CommonCollections-Exploit/ 提交漏洞总是要证明漏洞危害,老外写的java代码又有bug,所以 ...

  2. CentOS7编译安装Nginx-1.8.1和编译参数

    CentOS7编译安装Nginx-1.8.1和编译参数 Web服务器Nginx    LNMP是一组众所周知的Web网站服务器架构环境,即由Linux+Nginx+MySQL+PHP(MySQL有时也 ...

  3. [资源] Getting started with Tensorflow

    Learning Website / Blog: Official Tutorial: https://www.tensorflow.org/tutorial Learn Tensorflow: ht ...

  4. python_way day13 sqlalchemy

    sqlalchemy 一对多 多对多 1.一对多 一.#创建表结构 class Host(Base): #所有的子类都继承这个基类 #创建表结构 __tablename__ = 'hosts' id ...

  5. 浅谈Android手机木马手工查杀

    这篇文章主要是浅谈,所以会从简单方面开始讲起. 关于手机木马查杀,有些人会说安装手机杀毒软件不就解决了吗? 其实不然.因为手机和PC不一样,手机反木马技术没有PC端那么强. 就算你把目前市面上的所有手 ...

  6. css3动画导航实现

    代码 <!DOCTYPE html> <!-- saved from url=(0223)file:///C:/Users/user/AppData/Local/Temp/HZ$D. ...

  7. 【linux 命令】:查看系统开机,关机时间【转载】

    转载原文:http://www.cnblogs.com/kerrycode/p/3759395.html 看Linux开机关机时间的方法(非常全面) 1: who 命令查看 who -b 查看最后一次 ...

  8. HTML JSP Servlet 的 相对路径 绝对路径

    HTML 相对路径 - 没有最前面的 /: 相对于当前文件,和OS一样 绝对路径 - 前面带 / : 相对于  http://<host>:port/ Servlet 相对路径 - 相对于 ...

  9. 静态库冲突的解决办法:duplicate symbol

    昨天在做微信sdk和xmpp的集成,发现各自单独集成没问题,一起集成却总报错,百度了好一会儿才知道,这应该是库冲突造成的问题 然后参考了很多文章,跟着敲敲一遍,却发现问题多多,最后主要综合结合了这两个 ...

  10. iOS开发 字符串添加行间距

    + (CGFloat)achiveWidthAttrString:(NSAttributedString *)attrString withHeight:(CGFloat)height { CGRec ...