题目描述:

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. 【转载】Ogre的内存分配策略

    原文:Ogre的内存分配策略 读这个之前,强烈建议看一下Alexandrescu的modern c++的第一章关于policy技术的解释.应该是这哥们发明的,这里只是使用. 首先列出涉及到的头文件:( ...

  2. Reading Csv Files with Text_io in Oracle D2k Forms

    Below is the example to read and import comma delimited csv file in oracle forms with D2k_Delimited_ ...

  3. Horizontal Toolbar With Navigational Buttons Form Sample For Oracle Forms 10g/11g

    Sharing an Oracle Form Htoolbar.fmb for Oracle Forms 10g/11g containing Horizontal Toolbar canvas an ...

  4. Create Hierarchical Tree To Control Records In Oracle Forms

    Download Source Code Providing an example form for creating hierarchical trees in Oracle Forms to co ...

  5. C#窗体->>随机四则运算

    用户需求: 程序能接收用户输入的整数答案,并判断对错程序结束时,统计出答对.答错的题目数量.补充说明:0——10的整数是随机生成的用户可以选择四则运算中的一种用户可以结束程序的运行,并显示统计结果.在 ...

  6. Spark.ML之PipeLine学习笔记

    地址: http://spark.apache.org/docs/2.0.0/ml-pipeline.html   Spark PipeLine 是基于DataFrames的高层的API,可以方便用户 ...

  7. Python基础学习笔记(一)入门

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-chinese-encoding.html 3. http://w ...

  8. Spring security3入门(转)

    http://kingxss.iteye.com/blog/1908011 补充类图: 注意:1.修改为链接mysql数据库:             2.代码地址:https://github.co ...

  9. iOS - UIStepper

    前言 NS_CLASS_AVAILABLE_IOS(5_0) __TVOS_PROHIBITED @interface UIStepper : UIControl @available(iOS 5.0 ...

  10. TAROT.

    /* * * */ #include<stdio.h> #include<stdlib.h> #include<time.h> int main() { int t ...