九度OJ-浙大机试-题目1437:To Fill or Not to Fill

这是一题贪心算法,采用的基本思想还是普通加油问题的基本思想的升级版(普通加油站加油问题见POJ2431),就是在邮箱可到达的范围内找到最便宜的加油站加油。

但是贪心算法最难的肯定是证明,目前还没有能力做到,留坑以后填吧。

算法步骤:

1、将两站不能到达(距离大于油箱加满油的最大距离)和初始站没有加油站的情况单独讨论

2、找可到达范围内是否有比当前站便宜的加油站

2.1、如果没有比当前站便宜的

油箱加满油,前往下一站。

2.2、如果有比当前站便宜的

2.2.1、如果当前油量可以到达终点直接前往

2.2.2、如果下一站比当前站便宜的是终点站(终点站的油价为0),加油到终点站的位置

2.2.3、否则就是加油到下一站比当前站便宜的地方

3、输出价格


题目1437:To Fill or Not to Fill

时间限制:1 秒

内存限制:128 兆

特殊判题:

提交:2714

解决:599

题目描述:

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
<pre name="code" class="cpp">#include<stdio.h>
#include<iostream>
#include<algorithm>
struct point
{
    double price;
    int dis;
};
point p[1005];
bool cmp(point a,point b)
{
    if(a.dis < b.dis)
        return true;
    else
        return false;
}
using namespace std;
int main()
{
    int cmax,d,davg,n;
    while(scanf("%d %d %d %d",&cmax,&d,&davg,&n)!=EOF)
    {
        int j,i;
        for(i = 0 ; i < n;i++)
        {
            scanf("%lf %d",&p[i].price,&p[i].dis);
        }
        p[n].price = 0.00;
        p[n].dis= d;
        sort(p,p+n,cmp);
        double fullkm = cmax *davg;
        double tank = 0.0;
        double pr = 0.0;
        int flag =0;
        for( i = 0 ; i <n ;)
        {
            //can not arrive the next stop
            if(fullkm<(p[i+1].dis-p[i].dis)||p[0].dis!=0&&!flag)
            {
                if(p[0].dis==0)
                    printf("The maximum travel distance = %.2lf\n",p[i].dis+fullkm);
                else
                    printf("The maximum travel distance = 0.00\n");
                flag =1;
                break;
            }
            else
            {
                int minn;
                minn = i;
                for(j = i+1;j<=n&&p[i].dis+fullkm> p[j].dis;j++)
                {
                    if(p[j].price<=p[minn].price)
                    {
                        minn =j;
                        break;
                    }
                }
                j =minn;
   //             cout<<"i"<<i<<"j"<<j<<endl;
                if(minn==i)
                //当前站是最小
                {
                    j++;
                            pr += (cmax - tank)*1.0*p[i].price;
                            tank = cmax;
                            tank -= (p[j].dis-p[i].dis)*1.0/davg;
                            //printf("2#%.2lf\n",pr);
                }
                else
                {
                    if(tank*davg + p[i].dis>=d)
                    {
                        i = n-1;
                        break;
                    }
                    else if(p[j].dis >= d)
                    //便宜的那站是终点站
                    {
                        pr += ((d-p[i].dis)*1.0/davg-tank)*p[i].price;
                        //printf("1#%.2lf\n",pr);
                        //printf("%.2lf\n",((p[j].dis-p[i].dis)*1.0/davg-tank)*p[i].price);
                         i = n-1;
                        break;
                    }
                    else
                    {
       //                    printf("3.1#%.2lf\n",pr);
                        if((p[j].dis-p[i].dis)-tank*davg>0)
                        //油箱里面的油不够
                        {
                            pr += (((p[j].dis-p[i].dis)*1.0/davg-tank)*p[i].price);
         //                   cout<<(p[j].dis-p[i].dis)<<endl;
                            tank = (p[j].dis-p[i].dis)*1.0/davg;
                            tank -= (p[j].dis-p[i].dis)*1.0/davg;
             //               printf("3#%.2lf\n",pr);
                        }
                        else
                        {
                            tank -= (p[j].dis-p[i].dis)*1.0/davg;
                        }
                    }

                }
                //cout<<"i"<<i<<"j"<<j<<endl;
                //printf("distance: %d\n",p[j].dis-p[i].dis);
                //printf("price:%.2lf\n",pr);
                i = j;
            }
        }
        //cout<<"i"<<i<<endl;
        if(i==n-1&&!flag)
            printf("%.2lf\n",pr);

    }
}


 
    

/*
测试数据
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
ANS:749.17

<pre name="code" class="cpp" style="color: rgb(51, 51, 51); font-size: 14px; line-height: 25px;">50 1300 12 8
8.00 1250
7.00 600
7.00 150
7.10 0
7.20 200
7.50 400
7.30 1000
6.85 300
ANS:754.58
 
    

50 1300 12 8
6.00 1250
7.00 690
7.00 750
7.10 1
7.20 710
7.50 20
7.30 1000
6.85 800
ANS:The maximum travel distance = 0.00

*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值