您的位置:首页 > 其它

ZOJ 3699 Dakar Rally

2017-07-30 09:39 176 查看

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: