ZOJ 3699 (贪心模拟)【加油站类模板】

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个加油站,一辆车,油箱容量告诉你,各个加油站的油价告诉你,让你计算到达终点的最小花费。

  解题思路都在代码注释。

  【AC代码】

 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
struct no
{
    long long  len,pre,price;
}node[100009];//数组大小注意了

int main()
{
    int m,n,t,num; 
    long long val;//可能会爆数据 开成long long 
    scanf("%d",&t);
    while(t--)
    {
        int flag=0;
        scanf("%d%lld",&num,&val);
        for(int i=0;i<num;i++)
        {
            scanf("%lld%lld%lld",&node[i].len,&node[i].pre,&node[i].price);
            if(node[i].len*node[i].pre>val) flag=1;//如果某两个加油站之间的距离耗油量大于油箱最大储量  直接输出impossible
        }
        if(flag)
        {
            printf("Impossible\n");
            continue;
        }
        long long ans=0;//最小花费
        long long extra=0;//油箱剩余量
        for(int i=0;i<num;)
        {
            int p=i+1;//下一个点=加油站开始查找比当前油价便宜的站点
            int sum=node[i].pre*node[i].len;//但前站到下一站的耗油量
            while(p<num && node[p].price>=node[i].price && val-sum>=node[p].len*node[p].pre)//一直查找  直到到达终点或者找到油价更便宜的站或者油箱没有了
            {
                sum+=node[p].pre*node[p].len;//总共需要的油量
                p++;
            }
            if(p>=num || node[p].price<node[i].price)//如果是终点或者找到更便宜的站 
            {
                if(extra>sum)  //剩余油量大于到下一个更便宜站点的耗油量
                {
                    extra-=sum;//扣掉这几站的耗的油
                }
                else  //不能到达下一个便宜站点
                {
                    ans += (sum-extra)*node[i].price;//加油钱等于剩下路程的耗油量减去当前油箱剩余油量乘以当前站点油价
                    extra=0;//到那个更便宜站后油箱刚好剩余0
                }
                i=p;//到达那个站点
            }
            else//如果没找到更便宜的
            {
                ans += node[i].price*(val-extra);//加油钱就是原来的加上把油箱加满花的钱
                extra = val-node[i].len*node[i].pre;//到达下一站后油箱剩余油量
                i++;下一站
            }
        }
        printf("%lld\n",ans);//注意输出lld
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值