//贪心算法解决加油站选择问题
//# 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. [iOS]如何给Label或者TextView赋HTML数据

    // // ViewController.m // text // // Created by 李东旭 on 16/1/22. // Copyright © 2016年 李东旭. All rights ...

  2. vss的ss.ini丢失或损坏导致的vss无法登录错误

    vss的ss.ini丢失或损坏导致的vss无法登录错误 Written in 2007-07-03 18:17 在vss使用过程中,不知道什么原因,会导至vss目录中的ss.ini文件损坏,此文件位于 ...

  3. windows下python脚本程序的运行

    c:\python33\python.exe c:\python33\trycoding.py

  4. Effective C++学习笔记 条款06:如不想使用编译器自动生成的函数,就该明确拒绝

    一.为驳回编译器自动提供的机能,可将相应成员函数声明为private并且不予实现.(如果你仅仅是自己不实现的话,编译器会帮你实现) 如: class A { public: A(const strin ...

  5. 1156. Two Rounds(dfs+背包)

    1156 求出每个联通块的黑白块数 然后再背包 二维的背包 要保证每个块都得取一个 写的有些乱.. #include <iostream> #include<cstdio> # ...

  6. poj 3368 Frequent values(RMQ)

    题目:http://poj.org/problem?id=3368 题意:给定n个数,顺序为非下降,询问某个区间内的数出现最多的数的 出现次数.. 大白书上的 例题..算是RMQ变形了, 对 原数组重 ...

  7. bzoj1717: [Usaco2006 Dec]Milk Patterns 产奶的模式

    后缀数组+二分答案+离散化.(上次写的时候看数据小没离散化然后一直WA...写了lsj师兄的写法. #include<cstdio> #include<cstring> #in ...

  8. [转]ASP.NET 页生命周期概述

    原文链接:http://msdn.microsoft.com/zh-cn/library/ms178472(v=vs.110).aspx  对应版本:.NET 4.0 ASP.NET 页运行时,此页将 ...

  9. Live555中RTP包的打包与发送过程分析

    这里主要分析一下,live555中关于RTP打包发送的部分.在处理完PLAY命令之后,就开始发送RTP数据包了(其实在发送PLAY命令的response包之前,就会发送一个RTP包,这里传输就已经开始 ...

  10. <三>面向对象分析之UML核心元素之参与者

    一:版型        --->在UML里有一个概念叫版型.有些书里也称类型,构造型.        --->这个概念是对一个UML元素基础定义的扩展.在同一个元素基础定义的基础上赋予特别 ...