ZOJ 3699 Dakar Rally

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3699


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


Author: OUYANG, Jialin
Contest: The 13th Zhejiang University Programming Contest


大意——有一个越野拉力赛,赛程包括许多个赛段。因为各种各样的情况,所以汽车各赛段每英里的耗油量和油价都不一样。现在,给你每个赛段的里程,油耗量,油价以及油箱容量,请你帮助参赛者决策,以便尽量少花油费。假设参赛者一开始汽车里面没有油,每个加油站的油无限以及参赛者必须完成所有赛段才算完成任务。


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


复杂度分析——时间复杂度:O(n^2),空间复杂度:O(n)


附上AC代码:


#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <map>
using namespace std;
typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
const double pi = acos(-1.0);
const double e = exp(1.0);
const int maxn = 100005;
LL mileage[maxn], consume[maxn], price[maxn];
// 存储每一个赛段的长度,单位长度耗油量以及单位容量油价
int n; // 表示赛段的个数
LL capacity; // 坦克的容量

int main()
{
	ios::sync_with_stdio(false);
	int T; // 样例的个数
	scanf("%d", &T);
	while (T--)
	{
		bool flag = 0; // 标记是否达标,即每个赛段是否能够通过
		scanf("%d%lld", &n, &capacity);
		for (int i=0; i<n; i++)
		{
			scanf("%lld%lld%lld", &mileage[i], &consume[i], &price[i]);
			if (mileage[i]*consume[i] > capacity)
				flag = 1; // 赛段i耗油量大于存储的油量,坦克不足以支撑到
						  // 下一个赛段,故不可能完成所有赛段
		}
		if (flag)
		{ // 所有油耗尽都不能到达下一个赛段,则失败
			printf("Impossible\n");
			continue;
		}
		LL min_price = 0; // 记录最小花费
		LL extra = 0; // 记录到达赛段i时坦克剩余油量
		for (int i=0; i<n;)
		{
			int j = i+1; // 从赛段i的下一个赛段开始寻找价格便宜的赛段
			int temp = mileage[i]*consume[i]; // 到达下一个便宜的赛段所需要的油量
			while (j<n && price[j]>=price[i] && capacity-temp>=mileage[j]*consume[j])
			{ // 向终点查找,直到找到一个比赛段i价格便宜的赛段j,或者是油耗尽了
				temp += mileage[j]*consume[j];
				j++;
			}
			if (j>=n || price[j]<price[i])
			{ // 找到赛段j的价格比赛段i便宜,只要把油加到能到达赛段j即可
				if (extra > temp) // 油很多,不需要花钱买
					extra -= temp;
				else
				{
					min_price += (temp-extra)*price[i]; // 油不够,只需买能达到赛段j的油即可
					extra = 0; // 油箱的油全部用完
				}
				i = j;
			}
			else
			{ // 油耗尽了都没有找到比赛段i价格便宜的赛段
				min_price += (capacity-extra)*price[i]; // 加满油,到下一个赛段
				extra = capacity-mileage[i]*consume[i]; // 到下一个赛段还剩多少油
				i++;
			}
		}
		printf("%lld\n", min_price);
	}
	return 0;
}


你可能感兴趣的:(ZOJ,贪心)