浙大2012上机 PAT 1033. To Fill or Not to Fill (25)

16 篇文章 0 订阅

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.

Input Specification:

Each input file contains one test case. 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.

Output Specification:

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.

Sample Input 1:
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
Sample Output 1:
749.17
Sample Input 2:
50 1300 12 2
7.10 0
7.00 600
Sample Output 2:
The maximum travel distance = 1200.00

该问题是一个贪心算法问题,设满缸油可行驶的距离为s,当前加油站的位置为p1,那么需要找到在s距离内第一个油价比当前加油站油价便宜的点p2,如果s距离内p1油价最便宜,则加满;否则,保证p1位置油缸内的油缸号可以导致p2位置。

代码如下:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int MAXN = 510;
int N;//总的加油站点数
//int maxD;//一次加满油所能走的最远距离
float maxD;
struct Station
{
    float price;
    float dis;
    bool operator<(const Station& rhs) const
    {
        return dis < rhs.dis;
    }
};
Station station[MAXN];
int nextSt(int s)//当前汽车所处的站点为s,寻找在其加满油能够到达的站点中的油价比其便宜的站点
{
    int nextindex = s;
    int maxL = station[s].dis + maxD;
    for (int i = s + 1; i < N; i++)
    {
        if (station[i].price <= station[s].price&&station[i].dis<maxL)//注意等于号
        {
            nextindex = i;
            break;//找到一个即可
        }
    }
    return nextindex;
}

//在汽车加满油能够到达的距离中,若没有价格比当前车站更便宜的,则加满油,到达那个能够到达的最远的站点
//我们在计算时,总是认为当汽车到达下一个站点(不是每一个加油点都是一个站点)时汽车里面的汽油已经用完!!!
//若没有用完,比如这种情况,则要作相关处理!!
int farestSt(int s)
{
    int farestindex(s);
    int maxL = station[s].dis + maxD;
    for (int i = s + 1; i < N; i++)
    {
        /*if (station[i].dis>maxL)
        {
            break;
        }
        else
        {
            farestindex = i;
        }*/
        if (station[i].dis>maxL)
        {
            break;
        }
        farestindex = i;
    }
    return farestindex;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("D:\\in.txt", "r", stdin);
    freopen("D:\\out.txt", "w", stdout);
#endif
    float cmax(0), D(0), Davg(0);
    scanf("%f%f%f%d", &cmax, &D, &Davg, &N);
    for (int i = 0; i < N; i++)
    {
        scanf("%f%f", &station[i].price, &station[i].dis);
    }
    station[N].price = 0;
    station[N].dis = D;
    N++;
    maxD = Davg*cmax;//maxD一定要定义成浮点数类型
    sort(station, station + N);
    //if (station[0].dis !=0)
    if (station[0].dis>0)
    {
        printf("The maximum travel distance = 0.00\n");
        return 0;
    }
    float priceAll(0);
    //这个地方一定要注意
    //curSt这个用法一定要注意
    int curSt= 0;
    for (int i = 0; i < N; i++)
    {
        if (station[curSt].dis == D)
            break;
        int index = nextSt(curSt);
        if (index != i)
        {
            priceAll += (station[index].dis - station[curSt].dis)*station[curSt].price;
            curSt = index;
            continue;
        }
        index = farestSt(curSt);
        if (index != curSt)
        {
            priceAll += maxD*station[curSt].price - 
                (maxD - (station[index].dis - station[curSt].dis))*station[index].price;
            curSt = index;
            continue;//这步很关键
        }
        else
        {
            printf("The maximum travel distance = %.2f", station[curSt].dis + maxD);
            return 0;
        }
    }
    printf("%.2f", priceAll / Davg);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值