ZOJ 3699 Dakar Rally (greedy)

Source: Internet
Author: User

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 a annual Dakar Series Rally raid type of off-road race, organized by the Amaury Sport Organiz ation. The off-road endurance race consists of a series of routes. In different routes, the competitors cross Dunes, muds, 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-minimize their payment on gas.

Assume that, the car begins with a empty tank and each gas station have infinite gas. The racers need to finish all the routes in order as the test case descripts.

Input

There is multiple test cases. The first line of input contains an integert (T ≤50) indicating the number of test cases. ThenT test Cases follow.

The first line of all case contains, 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 theith route, which means the number of gas unit the car Consumes in 1 mile; PRICEI-The price of unit gas in the which locates at the beginning of theith route.

All integers is positive and no more than 105.

Output

For each test case, print the minimal cost-to-finish all of then routes. If it ' s impossible, print "impossible" (without the quotes).

Sample Input
22 305 6 94 7 102 305 6 94 8 10
Sample Output
550Impossible


The main idea: to arrange a refueling plan for the racers, so that they can ride all the routes of the least cost of money, of course, if the middle of the oil is not enough to output impossible said can not be completed. The first line T represents the number of samples, followed by a line n,m,n that there are so many roads, M represents the capacity of the car tank. Each subsequent line will have three numbers, indicating that the route length Len , the oil required to consume gas, the section of petrol station oil price . To run out of the lowest consumption of all routes.

problem-solving ideas: using greedy ideas. is the full name of the oil price to the lowest. It is obvious that the racers have completed each route sequentially, so we just need to consider how to refuel. Refueling in two cases: 1) Assuming that I refueling, until the use of oil can not find I point oil price is also cheap, then the oil full, until exhausted. 2) If the oil is not finished, you can find the lower cost of oil in J, so long as the amount of oil can reach the J point. Note: The fuel tank is empty at the start, to ensure that the end of the oil is just used.

Complexity Analysis: Time complexity: O (n*n)

Space complexity: O (n)



<span style= "FONT-SIZE:18PX;" > #include <iostream> #include <cstring> #include <cmath> #include <algorithm> #include < Cstdio>using namespace Std;typedef long long ll;const int maxn=100010;int len[maxn],gas[maxn],price[maxn];int main ()    {int N;    LL n,v;    scanf ("%d", &n);        while (n--) {int flag=0;//is used to indicate that if a route cannot be completed, Mark 1 scanf ("%lld%lld", &n,&v);            for (int i=0;i<n;i++) {scanf ("%lld%lld%lld", &len[i],&gas[i],&price[i]);        if (len[i]*gas[i]>v) flag=1;            } if (flag) {printf ("impossible\n");        Continue        } int i=0;        Int J; LL R=0,T,ANS=0;//R Record the amount of oil remaining in the fuel tank before refueling, ans is the lowest cost while (i<n) {j = i+1;//is the t=len[of the route completed sequentially i]*gas[i];//finish I route need to consume the amount of oil while (J<n && price[j]>=price[i] && v-t>=gas[j]*len[j])//If not found                                       To lower oil prices and to reach                            Next Station  {T +=len[j]*gas[j];//the amount of oil and ++j to be consumed before the next refueling. if (J>=n | | price[j]<price[i])//Find a petrol station with lower oil price {if (r>t) r-=t;//if the remaining                    Surplus of oil is enough to directly subtract the consumption, the station oil price is lower, in this station to supplement else{ans+= (T-r) *price[i];//The remaining oil is not enough, then in the last refueling, add a just arrived at the station                r=0;            } i=j; } else {ans+= (v-r) *price[i];//If you don't find a low oil price until you run out of oil, it was last added to full r=v-len[i]*gas[.            i];//the remaining oil volume i++;    }} printf ("%lld\n", ans); } return 0;} </span>



Copyright NOTICE: This article for Bo Master original article, reprint remember famous source, thank you!

ZOJ 3699 Dakar Rally (greedy)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.