ZOJ 3699 Dakar Rally(貪心),zojdakar

來源:互聯網
上載者:User

ZOJ 3699 Dakar Rally(貪心),zojdakar

題意  路上有 n 個加油站  每個加油站的價格可能不同  你的油箱容積為 v  問從起點開車到終點加油至少用多少錢

貪心  每次都讓油箱裡面便宜的油最多就行了   在每個網站 i 有兩種情況

1.  i 點把油加滿跑完都沒有更便宜的  那麼在 i 點肯定要加滿  然後開到 i+1 點去

2.  i 點把油加滿能跑到第一個比 i 點更便宜的 j 點或者到了終點 j   那麼只用把油加到能到 j 點  然後直接開到 j 點

然後把每個點加油用的錢加起來就行了

#include <cstdio>using namespace std;const int N = 100005;typedef long long ll;ll len[N], cost[N], pri[N], v;int main(){    int T, n;    scanf("%d", &T);    while(T--)    {        scanf("%d%lld", &n, &v);        int flag = 0;        for(int i = 0; i < n; i++)        {            scanf("%lld%lld%lld", &len[i], &cost[i], &pri[i]);            if(len[i] * cost[i] > v) flag = 1;        }        if(flag)        {            puts("Impossible");            continue;        }        int i = 0, j;        ll r = 0, ans = 0, t;                 //r記錄到i點還剩多少油        while(i < n)        {            j = i + 1, t = len[i] * cost[i];  //t記錄到下一個便宜的點用了多少油            //往前走直到找到一個更便宜的站j  或者油箱跑完            while( j < n && pri[j] >= pri[i] && v - t >= cost[j] * len[j])            {                t += len[j] * cost[j];                ++j;            }            if( j >= n || pri[j] < pri[i])  //j點油第一個比i點便宜  加到能到j            {                if(r > t)  r -= t;                else                {                    ans += (t - r)  * pri[i];                    r = 0;                }                i = j;            }            else  //油跑完都沒更便宜的 加滿 到下一站            {                ans +=  (v - r) * pri[i];                r = v - len[i] * cost[i];                i++;            }        }        printf("%lld\n", ans);    }    return 0;}

Dakar RallyTime Limit: 2 Seconds      Memory Limit: 65536 KBDescription

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
22 305 6 94 7 102 305 6 94 8 10
Sample Output
550Impossible


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.