//贪心算法解决加油站选择问题
//# 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. HDU5093——Battle ships(最大二分匹配)(2014上海邀请赛重现)

    Battle ships Problem DescriptionDear contestant, now you are an excellent navy commander, who is res ...

  2. android从应用到驱动之—camera(2)---cameraHAL的实现

    本文是camera系列博客,上一篇是: android从应用到驱动之-camera(1)---程序调用流程 本来想用这一篇博客把cameraHAL的实现和流程都给写完的.搞了半天,东西实在是太多了.这 ...

  3. MSRA面试总结

    http://blog.renren.com/share/405984844/16014442499 注:以下内容纯凭记忆,由于已经过去一个多月,不保证准确性.由于面试前没有签保密协议,本文透露了比较 ...

  4. Java开发工具MyEclipse的设置自动联想功能

    最近初学Java,正在使用MyEclipse来编写新的项目,刚开始打开MyEclipse感觉这个工具既陌生又熟悉,熟悉之处在于编辑器的几大共通之处它都具备,比如说基本的设置.编辑区.调试区都是类似的, ...

  5. csv,txt,excel文件之间的转换,perl脚本

    最近接触一些需要csv,txt,excel文件之间的转换,根据一些网上搜索加上自己的改动,实现自己想要的结果为主要目的,代码的出处已经找不到了,还请见谅,以下主要是针对csv&excel 和t ...

  6. TCSRM 591 div2(1000)(dp)

    挺好的dp 因为有一点限制 必须任意去除一个数 总和就会小于另一个总和 换句话来说就是去除最小的满足 那么就都满足 所以是限制最小值的背包 刚开始从小到大定住最小值来背 TLE了一组数据 后来发现如果 ...

  7. 一个简单的iBatis入门例子

    一个简单的iBatis入门例子,用ORACLE和Java测试 目录结构: 1.导入iBatis和oracle驱动. 2.创建类Person.java package com.ibeats;import ...

  8. HDU 4965 矩阵快速幂

    顺手写了下矩阵类模板 利用到矩阵乘法的交换律 (A*B)^n == A * (B*A)^n-1 *B #include <cstdio> #include <cstring> ...

  9. 手动配置gradle

    最近从github倒入项目,运行的特别慢gradle配置有问题,解决方法: 1.C:\android\demo\hellocharts-android-master\gradle\wrapper 目录 ...

  10. javascript 命名空间的实例应用

    /** * 创建全局对象MYAPP * @module MYAPP * @title MYAPP Global */ var MYAPP = MYAPP || {}; /** * 返回指定的命名空间, ...