Dakar Rally 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 1e5.

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段路,车的油箱容量为v,每段路长len,每走单位路长耗油cost,在每段路的开始可以加油,加油的价格为val,问能否走完这n段路?能的话输出最小花费

思路:我们很容易知每段路如果len*cost>v的话必不可能走完。我们贪心找当前能跑到的路段中油价比当前价格低的路,如果找到比当前价格低的路段,那么令油箱里的油刚好到达这个路,油箱里的油当前有两种状态,一种是油箱里剩余的油直接可以到达,那么不需要加油就可以到达,否则加刚好到达该路的油量,并将剩余油量变为0;如果没找到,如果到达终点,油箱里的油当前有两种状态,一种是油箱里剩余的油直接可以到达终点,那么不需要加油就可以到达,否则加刚好到达终点的油量,并将剩余油量变为0;如果没有到达终点,那么让车走到当前路段的下一个路,然后再用相同方法找。

#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;
struct node{
	ll len;
	ll cost;
	ll val; 
}a[100009];
int main()
{
	int t,n,flag;
	ll v;
	scanf("%d",&t);
	while(t--)
	{
		flag=0;
		scanf("%d%lld",&n,&v);
		for(int i=1;i<=n;i++)
		{
			scanf("%lld%lld%lld",&a[i].len,&a[i].cost,&a[i].val);
			if(a[i].len*a[i].cost>v)
				flag=1;
		}
		if(flag)
		{
			printf("Impossible\n");
			continue;
		}
		ll ans=0,left=0;
		for(int i=1;i<=n;)
		{
			int j=i+1;
			ll tmp=a[i].cost*a[i].len;
			while(j<=n&&a[j].val>=a[i].val&&v-tmp>=a[j].cost*a[j].len)
			{
				tmp+=a[j].cost*a[j].len;
				j++;
			}
			if(j>n)
			{
				if(left>tmp)
					left-=tmp;
				else
				{
					ans+=(tmp-left)*a[i].val;
					left=0;
				}
				break;
			}
			else if(a[j].val<a[i].val)
			{
				if(left>tmp)
					left-=tmp;
				else
				{
					ans+=(tmp-left)*a[i].val;
					left=0;
				}
				i=j;
			}
			else
			{
				ans+=(v-left)*a[i].val;
				left=v-a[i].cost*a[i].len;
				i++;
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值