//贪心算法解决加油站选择问题
//# include<iostream>
# include<stdio.h>
using namespace std; # include<algorithm> struct Node
{
float p, d;
};
bool cmp(Node a, Node b)
{
return a.d < b.d;
} int main()
{
Node node[];
float Cmax, D, Davg, distance, price, Ccur, Pcur, j;//double 会出问题,蛋疼
int N, Ncur, i, k, flag;
//while (cin >> Cmax >> D >> Davg >> N)
while (scanf_s("%f%f%f%d", &Cmax, &D, &Davg, &N) != EOF)
{
for (i = ; i <= N; i++)
{
//cin >> node[i].p >> node[i].d;
scanf_s("%f%f", &node[i].p, &node[i].d);
}
//sort station by distance
sort(node + , node + + N, cmp); if (N == || node[].d > 0.00001 || node[].d < -0.00001)//第一个站点不在距离0处
{
printf("The maximum travel distance = 0.00\n");
}
else//greedy now
{
price = ;//当前花费
distance = ;//当前距离
Ncur = ;//当前所在加油站
Ccur = ;//当前汽油数量
Pcur = node[].p;//当前加油站的油价
while (Ncur <= N)
{
//1.using 【remain】 gas find 【nearest】 【cheaper】 station
flag = -;
for (i = Ncur + ; i <= N&&node[i].d <= distance + Ccur*Davg; i++)
{
if (node[i].p < Pcur)
{
flag = i;
break;
}
}
if (flag != -)//find a station,and get there
{
//price 未变
Ccur -= (node[flag].d - distance) / Davg;//当前汽油数量===注意与下面表达式的计算顺序
distance = node[flag].d;//当前距离
Ncur = flag;//当前所在加油站
Pcur = node[flag].p;//当前加油站的油价
continue;
}
//else 1: 2.using 【Cmax】 find 【nearest】 【cheaper】 station
flag = -;
for (i = Ncur + ; i <= N&&node[i].d <= distance + Cmax*Davg; i++)
{
if (node[i].p < Pcur)
{
flag = i;
break;
}
}
if (flag != -)//find a station,and get there
{
price += ((node[flag].d - distance) / Davg - Ccur)*node[Ncur].p;//当前花费
distance = node[flag].d;//当前距离
Ncur = flag;//当前所在加油站
Pcur = node[flag].p;//当前加油站的油价
Ccur = ;//当前汽油数量:到K后的汽油为0
continue;
}
//else 2: 3.get the Cmax and go as far as possible
flag = -;
for (i = Ncur + ; i <= N&&node[i].d <= distance + Cmax*Davg; i++)
{
flag = i;
}
if (flag != -)//can get some one station,and get there
{
price += (Cmax - Ccur)*node[Ncur].p;//当前花费
distance = node[flag].d;//当前距离
Ncur = flag;//当前所在加油站
Pcur = node[flag].p;//当前加油站的油价
Ccur = Cmax - (node[flag].d - distance) / Davg;//当前汽油数量
continue;
}
else//can not get some one station,over
{
printf("The maximum travel distance = %.2f\n", distance + Cmax*Davg);
break;
}
}
if (Ncur > N)
{
printf("%.2lf\n", price);
} }
}
return ;
}

题目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

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

  3. 1033. To Fill or Not to Fill (25)

     题目链接:http://www.patest.cn/contests/pat-a-practise/1033 题目: 1033. To Fill or Not to Fill (25) 时间限制 1 ...

  4. 【贪心】PAT 1033. To Fill or Not to Fill (25)

    1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 ZHANG, Gu ...

  5. PAT 1033 To Fill or Not to Fill[dp]

    1033 To Fill or Not to Fill(25 分) With highways available, driving a car from Hangzhou to any other ...

  6. PAT 甲级 1033 To Fill or Not to Fill (25 分)(贪心,误以为动态规划,忽视了油量问题)*

    1033 To Fill or Not to Fill (25 分)   With highways available, driving a car from Hangzhou to any oth ...

  7. PAT_A1033#To Fill or Not to Fill

    Source: PAT A1033 To Fill or Not to Fill (25 分) Description: With highways available, driving a car ...

  8. PAT甲级——1033 To Fill or Not to Fill

    1033 To Fill or Not to Fill With highways available, driving a car from Hangzhou to any other city i ...

  9. PAT A1033 To Fill or Not to Fill (25 分)——贪心

    With highways available, driving a car from Hangzhou to any other city is easy. But since the tank c ...

随机推荐

  1. C#基础练习(时间的三连击)

    Form1的后台代码: namespace _07事件的三连击 {     public partial class Form1 : Form     {         public Form1() ...

  2. 59. Spiral Matrix II

    题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...

  3. javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint

    使用hibernate validator出现上面的错误, 需要 注意 @NotNull 和 @NotEmpty  和@NotBlank 区别 @NotEmpty 用在集合类上面@NotBlank 用 ...

  4. android4.4内核移植

    01 init/目录下Kconfig修改: 956行添加: config PANIC_TIMEOUT int "Default panic timeout" help Set de ...

  5. Pig简单入门

    pig是hadoop客户端,使用类似于SQL的面向数据流的语言pig latin,这个语言可以完成排序,过滤,求和,关联等操作,可以支持自定义函数.Pig自动把pig latin 映射为Map-Red ...

  6. mac 下 apache设置

    windows下面的apache配置 apache是mac下是默认就有的,我们只需使用命令开启.暂停和重启就好了 sudo apachectl start sudo apachectl stop su ...

  7. bzoj2875: [Noi2012]随机数生成器

    矩阵乘法. x[n] = {x[0],1} * ( {a,0} ^ n ) {b,1} 写成这样谁能看懂.... noi里的大水题.我居然 #include<cstdio> #includ ...

  8. EF4.0和EF5.0增删改查的写法区别及执行Sql的方法

    EF4.0和EF5.0增删改查的写法区别 public T AddEntity(T entity) { //EF4.0的写法 添加实体 //db.CreateObjectSet<T>(). ...

  9. mysql 索引与优化like查询

    索引与优化like查询 1. like %keyword    索引失效,使用全表扫描.但可以通过翻转函数+like前模糊查询+建立翻转函数索引=走翻转函数索引,不走全表扫描. 2. like key ...

  10. jQuery实战读书笔记(备忘录)

    选择器备忘: | :even 匹配所有索引值为偶数的元素,从 0 开始计数 :odd 匹配所有索引值为奇数的元素,从 0 开始计数 实例——设置table交替行变色: <script type= ...