ZOJ3699-Dakar Rally

22 篇文章 0 订阅

Dakar Rally

Time Limit: 2 Seconds       Memory Limit: 65536 KB

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个赛段,告诉你油箱容量,给你每个赛段的里程,油耗量,油价。一开始汽车里面没有油,每个加油站的油无限以及参赛者必须完成所有赛段才算完成任务,问最少花多少油费

解题思路:首先,假如某个赛段的总油耗量大于油箱容量,那么参赛者是不可能完成本赛段的,亦即不可能完成任务。要油费最少,那么每次保证油箱里面的油便宜的最多就行了。然后要考虑的就是在第i站要不要加油,怎么加。加的有两种情况出现:1.在第i点把油加满,直到用完都没能找到比i点价格便宜的,那么果断加满,开到下一点去;2.在第i点把油加满,没用完之前能够找到比i点便宜的点,或者是到达终点,那么只要将油加到能到达这个点即可,然后直接到达这个点。最后,把每个点所需油费加起来即可


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
#include <functional>

using namespace std;

#define LL long long
const int INF=0x3f3f3f3f;

LL l[100005],a[100005],price[100005];
int n;
LL m;

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int flag=0;
        scanf("%d%lld",&n,&m);
        for (int i=0; i<n; i++)
        {
            scanf("%lld%lld%lld",&l[i],&a[i],&price[i]);
            if(l[i]*a[i]>m) flag=1;
        }
        if(flag)
        {
            printf("Impossible\n");
            continue;
        }
        LL mi=0,extra=0;
        for(int i=0;i<n;)
        {
            int j=i+1;
            int temp=l[i]*a[i];
            while(j<n&&price[j]>=price[i]&&m-temp>=l[j]*a[j])
            {
                temp+=l[j]*a[j];
                j++;
            }
            if(j>=n||price[j]<price[i])
            {
                if(extra>temp) extra-=temp;
                else
                {
                    mi+=(temp-extra)*price[i];
                    extra=0;
                }
                i=j;
            }
            else
            {
                mi+=(m-extra)*price[i];
                extra=m-l[i]*a[i];
                i++;
            }
        }
        printf("%lld\n",mi);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值