zoj 3699 Dakar Rally(单调队列)

来源:互联网 发布:js split方法 编辑:程序博客网 时间:2024/04/28 01:17

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 theith 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 gas station which locates at the beginning of theith 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

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;#define 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;}









0 0
原创粉丝点击