题目地址:http://ac.jobdu.com/problem.php?pid=1437

题目描述:

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
/*
* Main.c
*
* Created on: 2014年1月18日
* Author: Shaobo
* greedy algorithm
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h> #define MAXN 501
#define MAXC 30000000.0 typedef struct station{
float price;
int dist;
}Station; int compare(const void * p, const void * q){
Station * p1 = (Station *)p;
Station * q1 = (Station *)q;
return p1->dist - q1->dist;
} int main(void){
int Cmax, D, Davg, N; //容量、距离、每单位气行驶的距离、加气站总数
int i;
Station sta[MAXN];
float sum, remind_gas, tmp;
int k, step; while (scanf("%d %d %d %d", &Cmax, &D, &Davg, &N) != EOF){
for (i=0; i<N; ++i){
scanf("%f %d", &sta[i].price, &sta[i].dist);
}
sta[N].dist = D;
sta[N].price = 1000000.0;
qsort(sta, N, sizeof(Station), compare); //按与杭州距离大小给加气站排序
if (sta[0].dist > 0){
printf ("The maximum travel distance = 0.00\n");
continue;
}
sum = 0; //总费用
step = Cmax*Davg; //加满油行驶最大距离
remind_gas = 0; //剩余油量
for (i=0; i<N; ++i){
k = i+1;
if (i != 0)
remind_gas -= ((float)(sta[i].dist -sta[i-1].dist))/Davg;
for (; k<N && sta[k].price>=sta[i].price; ++k)
continue;
if (sta[k].dist-sta[i].dist > step){
sum += (Cmax-remind_gas)*sta[i].price;
remind_gas = Cmax;
}
else{
tmp = ((float)(sta[k].dist-sta[i].dist))/Davg - remind_gas;
if (fabs(tmp)>1e-5 && tmp>0){
sum += tmp*sta[i].price;
remind_gas = ((float)(sta[k].dist-sta[i].dist))/Davg;
}
}
if (sta[i+1].dist - sta[i].dist > step){
printf ("The maximum travel distance = %.2f\n", (float)(sta[i].dist+step));
break;
}
}
if (i == N){
printf ("%.2f\n", sum);
}
} return 0;
}

九度OJ 1437 To Fill or Not to Fill -- 贪心算法的更多相关文章

  1. 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)

    题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述:     省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...

  2. 九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题

    题目1437:To Fill or Not to Fill 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1488 解决:345 题目描述: With highways availabl ...

  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. 九度OJ 1504 把数组排成最小的数【算法】-- 2009年百度面试题

    题目地址:http://ac.jobdu.com/problem.php?pid=1504 题目描述: 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如 ...

  6. 九度OJ 1172:哈夫曼树 (贪心)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6701 解决:2954 题目描述: 哈夫曼树,第一行输入一个数n,表示叶结点的个数.需要用这些叶结点生成哈夫曼树,根据哈夫曼树的概念,这些结 ...

  7. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  8. 九度OJ 1502 最大值最小化(JAVA)

    题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...

  9. 九度OJ,题目1089:数字反转

    题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...

随机推荐

  1. OpenSSH 高级运用两则

    00×0.相关介绍 OpenSSH(OpenBSD Secure Shell)使用 SSH 通过计算机网络加密通信的实现. 它是替换由 SSH Communications Security 所提供的 ...

  2. storm-starter项目概述

    storm-starter项目包含使用storm的各种各样的例子.项目托管在GitHub上面,其网址为: http://github.com/nathanmarz/storm-starter stor ...

  3. 问题-[DelphiXE2]提示第三控件不存在

    问题情况:在DelphiXE2启动时界面显示加载了控件,并且控件的路径也放在了环境变量中,但打开程序报第三控件不存在. 问题原因:是没有选择要加载的控件. 问题处理:点击Component->I ...

  4. ip_conntrack缓存neighbour

    在我的ip_conntrack版本中,它目前已经可以缓存路由,filter规则等,还可以平滑生效最新配置的NAT,它越来越像真正的SDN了,唯一有待完善的就是将5元组的tuple进化成N元组的tupl ...

  5. 8-14-Exercise

    8-14-小练 这次是我这组出题......我出的是B.C.D[虽然本来是想出的很难......╮(╯▽╰)╭但是,没找到AC1000+同时又让我想出的难题......SO...我出的真的不难= =] ...

  6. IOS开发UIImage中stretchableImageWithLeftCapWidth方法的解释

    - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCap ...

  7. VS中调用SQL SERVER存储过程

     存储过程是经过编译的,永久保存在数据中的一组SQL语句,通过创建和使用存储过程能够提高程序的重用性和扩展性,为程序提供模块化的功能,还有利于对程序的维护和管理.以下就详谈一下,VB.NET怎样调 ...

  8. GoogleProgressBar

    https://github.com/jpardogo/GoogleProgressBar

  9. WEB网站常见受攻击方式及解决办法

    一个网站建立以后,如果不注意安全方面的问题,很容易被人攻击,下面就讨论一下几种漏洞情况和防止攻击的办法. 一.跨站脚本攻击(XSS) 跨站脚本攻击(XSS,Cross-site scripting)是 ...

  10. LUN 和 LVM 知识

    LUN是对存储设备而言的,volume是对主机而言的. lun是指硬件层分出的逻辑盘,如raid卡可以将做好的400G的raid5再分成若干个逻辑盘,以便于使用,每一个逻辑盘对应一个lun号,OS层仍 ...