题目描写叙述:

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
来源:

2012年浙江大学计算机及软件project研究生机试真题

这道题确实挺难的,花了好久的时间,然后自己考虑不全面,最后參考别人的代码才搞定。

就不敢写原创了。。。

http://ziliao1.com/Article/Show/73A96AF77079A6C32C4AA82604FCF691

典型的贪心法。

思想就是考虑下一次在何网站加油(从而决定了在本网站须要加多少油)。

考虑这样几种情况:

1、到达下一网站所需油量 > 油箱最大容量:则下一网站不可达。做法是把油箱加满,尽可能跑,然后break掉。

2、下一网站可达,且油价比本网站廉价:则应尽早“换用”更廉价的油。做法是本站加够就可以。使得刚好能到达下一站。

3、下一网站可达。但油价比本网站贵:此处第一次做错了,不应该在本站把油箱加满,而应该继续寻找满油的条件下可达的下一个比本站廉价的网站。若找到,则加够就可以(所以情况2能够并到这里);若未找到,则在本站将油箱加满。

#include <algorithm>#include <iomanip>
#include <iostream>
using namespace std;
struct station
{
float price;
float dist;
};
station st[501];
float cmax, d, davg;
int n;
bool cmp(station a, station b)
{
return a.dist < b.dist;
}
// 寻找下一个可达的廉价网站
int nextCheaper(int now)
{
for(int i = now; i < n; i++)
{
if(st[i].dist - st[now].dist > cmax * davg) break;
if(st[i].price < st[now].price)
return i;
}
return -1;
}
int main()
{
while(cin >> cmax)
{
cin >> d >> davg >> n;
for(int i = 0; i < n; i++)
cin >> st[i].price >> st[i].dist;
st[n].price = -1; st[n].dist = d;
n = n + 1;
sort(st, st + n, cmp);
int nowst = 0;
float nowgas = 0;
float cost = 0;
while(nowst < n - 1)
{
if(nowst == 0 && st[0].dist != 0)
{
st[nowst].dist = 0; break;
}
float needgas = (st[nowst + 1].dist - st[nowst].dist) / davg;
if(needgas > cmax)
{
float addgas = cmax - nowgas;
cost += addgas * st[nowst].price;
st[nowst].dist += cmax * davg;
break;
}
int nextc = nextCheaper(nowst);
if(nextc == -1)
{
float addgas = cmax - nowgas;
nowgas = cmax;
cost += addgas * st[nowst].price;
nowgas -= needgas;
nowst = nowst + 1;
}else{
needgas = (st[nextc].dist - st[nowst].dist) / davg;
float addgas = needgas - nowgas;
if(addgas > 0)
{
nowgas += addgas;
cost += addgas * st[nowst].price;
}
nowgas -= needgas;
nowst = nextc;
}
}
if(nowst == n - 1)
cout << fixed << setprecision(2) << cost << endl;
else{
float maxdist = st[nowst].dist;
cout << "The maximum travel distance = "<< fixed << setprecision(2) << maxdist << endl;
}
}
return 0;
}

以下是我模仿大神自己手写的代码,差点儿都改动的全然一样了。。。眼下还是过不了。郁闷

有时间再研究一下啦。。。如今 真的发现不了什么错误了

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct station
{float pri;float dis;
}a[999];
int c,d,davg,n;
int cmp(station t1,station t2)
{return t1.dis<t2.dis;
}
int next(int now)
{for(int i=now+1;i<=n&&a[i].dis-a[now].dis<=c*davg;i++){if(a[i].pri<a[now].pri)return i;}return -1;
}int main()
{int i;while(cin>>c>>d>>davg>>n){for(i=0;i<n;i++){scanf("%f %f",&a[i].pri,&a[i].dis);}a[n].pri=-1;a[n].dis=d;sort(a,a+n,cmp);// for(i=0;i<n;i++)//printf("%f %f\n",a[i].dis,a[i].pri);int nowst=0;float anspri=0,ansdis=0,nowgas=0;while(nowst<n){if(nowst==0&&a[0].dis!=0){ansdis=0;break;}float needgas=(a[nowst+1].dis-a[nowst].dis)/davg;if(needgas>c){ansdis+=davg*c;break;}int nextst=next(nowst);//   printf("%d %d %f\n",nowst,nextst,anspri);if(nextst==-1){anspri+=(c-nowgas)*a[nowst].pri;nowgas=c-needgas;ansdis=a[nowst+1].dis;nowst+=1;}else{float addgas=(a[nextst].dis-a[nowst].dis)/davg-nowgas;if(addgas>0){nowgas=0;anspri+=addgas*a[nowst].pri;}elsenowgas-=needgas;nowst=nextst;ansdis=a[nowst].dis;}}if(nowst==n)printf("%.2f\n",anspri);elseprintf("The maximum travel distance = %.2f\n",ansdis);}return 0;
}/**************************************************************Problem: 1437User: HCA1101Language: C++Result: Wrong Answer
****************************************************************/

转载于:https://www.cnblogs.com/gcczhongduan/p/5266838.html

九度OJ #1437 To Fill or Not to Fil相关推荐

  1. 九度OJ 1437 To Fill or Not to Fill -- 贪心算法

    题目地址:http://ac.jobdu.com/problem.php?pid=1437 题目描述: With highways available, driving a car from Hang ...

  2. 打不开磁盘配额linux,九度OJ 1455 珍惜现在,感恩生活 -- 动态规划(背包问题)...

    题目描述: 为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品,其价格不等,并且只能整袋购买.请问:你用有限的资金最 ...

  3. Freckles - 九度 OJ 1144

    Freckles - 九度 OJ 1144 题目 时间限制:1 秒 内存限制:128 兆 特殊判题:否 题目描述: In an episode of the Dick Van Dyke show, l ...

  4. 非常可乐(九度 OJ 1457)

    非常可乐(九度 OJ 1457) 时间限制:1 秒 内存限制:32 兆 特殊判题:否 1.题目描述: 大家一定觉的运动以后喝可乐是一件很惬意的事情,但是 seeyou 却不这么认为.因为每次当 see ...

  5. 九度OJ 题目1179:阶乘

    /********************************* * 日期:2013-2-8 * 作者:SJF0115 * 题号: 九度OJ 题目1179:阶乘 * 来源:http://ac.jo ...

  6. 九度OJ——1028继续畅通工程

    题目描述: 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).现得到城镇道路统计表,表中列出了任意两城镇间修 ...

  7. Leagal or Not - 九度 OJ 1448

    Leagal or Not - 九度 OJ 1448 题目 时间限制:1 秒 内存限制:128 兆 特殊判题:否 题目描述: ACM-DIY is a large QQ group where man ...

  8. 九度OJ 题目1069:查找学生信息 随笔

    ** 九度OJ 题目1069:查找学生信息 ** 题目描述如下: 输入N个学生的信息,然后进行查询. 输入 输入的第一行为N,即学生的个数(N<=1000) 接下来的N行包括N个学生的信息,信息 ...

  9. 九度OJ 题目1203:IP地址

    /********************************* * 日期:2013-2-8 * 作者:SJF0115 * 题号: 九度OJ 题目1203:IP地址 * 来源:http://ac. ...

最新文章

  1. Firefox 控制台
  2. 首次使用计算机鼠标键盘不能用,解决方案:如何解决无法使用计算机鼠标和键盘的问题?如果启动后无法使用鼠标和键盘该怎么办?...
  3. java 多线程的基本概念_java基本教程之多线程基本概念 java多线程教程
  4. Hyperledger Fabric基础知识
  5. oracle表名最大长度6,Oracle中表名的最大长度是多less?
  6. 近5年133个Java面试题 你会几个?
  7. NGINX 配置超时时间
  8. Matpower安装流程
  9. 将路由器作为AP组建局域网的方法
  10. 【小笔记】大数据量excel解析工具性能对比
  11. 上传图片时出现http 415错误
  12. 记一次武汉科技大学ctf新手赛 wuctf2020
  13. 「CF1154F」Shovels Shop【背包DP】
  14. 慈航公益仲恺义工大区和爱心企业助力探亲日慈善活动
  15. Java中的移位操作——Java编程思想笔记
  16. NDN助力网络5.0时代
  17. Linux上vim编辑器使用教程
  18. 有关非居民企业就来源于中国境内的所得缴纳企业所得税问题
  19. Linux培训哪家靠谱?过来人教你如何挑选培训机构
  20. flash在线拍照并上传到后台(servlet)

热门文章

  1. 手机恢复出厂设置命令_擦除数据/恢复出厂设置通过ADB
  2. List的五种去重方式
  3. 使用jquery获取url及url参数的方法及定义JQuery扩展方法
  4. Ubuntu 8.04下Netbeans的字体反锯齿解决(转)
  5. 基于Response的将数据导出到Excel
  6. 1.怎样定制VC#DataGrid列标题?
  7. 只腐蚀毛刺 腐蚀算法_图像的腐蚀 膨胀及细化
  8. 控制反转_Spring:IOC 控制反转
  9. Python爬虫自学之第(①)篇——爬虫伪装和反“反爬”
  10. 前后端分离项目,后端是如何处理前端传递的token?