题目地址: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. HDOJ-ACM1019(JAVA) 多个数的最小公倍数

    题意:求多个数的最小公倍数 很简单,但是我一开始的做法,估计会让结果越界(超过int的最大值) import java.util.*; import java.io.*; public class M ...

  2. POJ2115 - C Looooops(扩展欧几里得)

    题目大意 求同余方程Cx≡B-A(2^k)的最小正整数解 题解 可以转化为Cx-(2^k)y=B-A,然后用扩展欧几里得解出即可... 代码: #include <iostream> us ...

  3. SpringTest 使用说明 -构建无污染纯绿色事务测试框架 (记录用)

    @ContextConfiguration({"classpath:applicationContext.xml","classpath:spring/buyer/app ...

  4. ural 1748 The Most Complex Number 和 丑数

    题目:http://acm.timus.ru/problem.aspx?space=1&num=1748 题意:求n范围内约数个数最多的那个数. Roughly speaking, for a ...

  5. How to change pager CSS in CGridView CListView in Yii

    类手册: http://www.yiiframework.com/doc/api/1.1/CLinkPager 其它参考: http://capstone3.blogspot.com/2012/06/ ...

  6. java实现下载文件到本地

    代码如下: URL url = new URL("http://www.cnblogs.com/images/logo_small.gif"); URLConnection con ...

  7. POJ3155 Hard Life

    Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 8482   Accepted: 2461 Case Time Limit:  ...

  8. Xenomai 的模式切换浅析

    在Xenomai的用户空间下,有两种模式:primary mode (主模式) 和 secondary mode(次模式). 在主模式下调用Linux系统调用后程序就会进入次模式,反之,在次模式下调用 ...

  9. contentProvider模板

    package com.example.qunzheng.todolist.provider; import android.content.ContentProvider; import andro ...

  10. jquery选择器及效率问题

    $('p2') //选择名字 $('.class') //选择class $('#id') //选择id $('#id li') //所有id=”id”标签内的li标签 $(“#id”).find(“ ...