Zoj 3699 Dakar Rally (monotone queue)

Source: Internet
Author: User

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 integer t (T ≤50) indicating the number of test cases. Then T 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 the n 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




#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack># include<vector> #include <set> #include <map> #define L (x) (x<<1) #define R (x) (x<<1|1) # Define MID (x, y) ((x+y) >>1) #define BUG printf ("hihi\n") #define EPS 1e-8typedef Long long ll;using namespace std; #def Ine N 100005#define INF 0x3f3f3f3fstruct stud{ll Len,price;}    F[n];int n;int all;ll money;int que[n],head,tail;int num[n];int need;void out (int pos) {int i,j; while (Head<tail&&f[que[tail-1]].price>=f[pos].price) {MONEY-=F[QUE[TAIL-1]].PRICE*NUM[QUE[TAIL-1]        ];        NEED+=NUM[QUE[TAIL-1]];    tail--;    }}inline void in (int pos) {que[tail]=pos;    Num[que[tail++]]=need; Money+=need*f[pos].price;}     inline void Run (int pos) {ll len=f[pos].len;         while (Len) {ll temp=len; if (temp>num[qUe[head]]) temp=num[que[head];         Num[que[head]]-=temp;         Len-=temp;     if (num[que[head]]==0) head++;    }}void solve () {int i,j;    head=tail=0;    money=0;    memset (num,0,sizeof (num));        for (i=1;i<=n;i++) {need= i==1? All:f[i-1].len;        Out (i);        In (i);    Run (i);        } while (Head<tail) {money-= (ll) Num[que[head]]*f[que[head]].price;    head++; } printf ("%lld\n", Money);}    int main () {int i,j,t,ca=0;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&all);        int x, y;        BOOL Flag=false;            for (i=1;i<=n;i++) {scanf ("%d%d%d", &x,&y,&f[i].price);            F[i].len= (ll) x*y;        if (F[i].len>all) flag=true;            } if (flag) {printf ("impossible\n");        Continue    } solve (); } return 0;}









Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Zoj 3699 Dakar Rally (monotone queue)

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.