ZOJ 3699 Dakar Rally

Description

The Dakar Rally is an annual Dakar Series rally raid type of off-road race, organized by the Amaury Sport Organization. The off-road endurance race consists of a series of routes. In different routes, the competitors cross dunes, mud, camel grass, rocks, erg and so on.

Because of the various circumstances, the mileages consume of the car and the prices of gas vary from each other. Please help the competitors to minimize their payment on gas.

Assume that the car begins with an empty tank and each gas station has infinite gas. The racers need to finish all the routes in order as the test case descripts.

Input

There are multiple test cases. The first line of input contains an integer T (T ≤ 50) indicating the number of test cases. Then T test cases follow.

The first line of each case contains two integers: n -- amount of routes in the race; capacity -- the capacity of the tank.

The following n lines contain three integers each: mileagei -- the mileage of the ith route; consumei -- the mileage consume of the car in the ith route , which means the number of gas unit the car consumes in 1 mile; pricei -- the price of unit gas in the gas station which locates at the beginning of the ith route.

All integers are positive and no more than 105.

Output

For each test case, print the minimal cost to finish all of the n routes. If it's impossible, print "Impossible" (without the quotes).

Sample Input
2
2 30
5 6 9
4 7 10
2 30
5 6 9
4 8 10
Sample Output
550
Impossible


题意:给n个加油站, 汽车油箱的容量,之后是每个加油站到下一个的距离,每公里耗油量, 油的单价。求走完所需的最少的金钱,如果不能走完输出Impossible。

思路:一道贪心题。

对于每个加油点判断是否能走到下个站点。

如果不能直接Impossible

如果能,判断下个站点的油价是否小于当前油价

如果小于,则只需在当前站点加油至刚好可以走到下一站点

如果不小于,则继续往后找,直到耗油量大于油箱容量,此时在当前站点加满。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=100005;
struct node
{
    int mile, cost, price;
}point[maxn];
int n, v;
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        bool flag=true;
        scanf("%d%d", &n, &v);
        for(int i=1; i<=n; i++)
        {
            scanf("%d%d%d", &point[i].mile, &point[i].cost, &point[i].price);
            if(point[i].mile*point[i].cost>v)     //判断是否能到达下一站点
                flag=false;
        }
        if(!flag)
        {
            printf("Impossible\n");
            continue;
        }
        long long sum=0;  //记录所需金钱
        long long t=0;    //记录到下个站点所需的油量
        long long r=0;   //记录剩余的油量
        int i=1;
        int j;
        while(i<=n)
        {
            j=i+1;
            t=point[i].mile*point[i].cost;
            while(j<=n&&point[j].price>=point[i].price&&v-t>=point[j].cost*point[j].mile)  //向后找油价便宜的点
            {
                t+=point[j].mile*point[j].cost;
                j++;
            }
            if(j>n||point[j].price<point[i].price)    //找到便宜的
            {
                if(r>t)
                    r-=t;
                else
                {
                    sum+=(t-r)*point[i].price;
                    r=0;
                }
                i=j;
            }
            else   //油箱加满都没找到的
            {
                sum+=(v-r)*point[i].price;
                r=v-point[i].mile*point[i].cost;
                i++;
            }
        }
        printf("%lld\n", sum);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值