ZOJ 3699

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

 肯定是想尽可能的在有便宜的时候多加,但是又不能有用不完浪费了的。

贪心做:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;

long long T,n,m,a[100010],b[100010],ans,t;
long long i,j,s;
bool flag;

int main()
{
    scanf("%lld",&T);
    while(T--)
    {
        flag = 1;
        scanf("%lld%lld",&n,&m);
        for(int i = 0;i < n; ++i)
        {
            int x,y;
            scanf("%lld%lld%lld",&x,&y,&b[i]);
            a[i] = x * y;
            if(a[i] > m) flag = 0;
        }
        if(!flag)
        {
            printf("Impossible\n");
            continue;
        }
        i = t = s = 0;
        ans = 0;
        while(i < n)
        {
            j = i + 1;
            t = a[i];
            while(j < n && b[j] >= b[i] && m - t >= a[j])
            {
                t += a[j];
                j++;
            }
            if(j < n && b[j] >= b[i])
            {
                ans += (m - s) * b[i];
                s = m - a[i];
                i++;
            }
            else
            {
                if(s > t) s -= t;
                else
                {
                    ans += (t - s) * b[i];
                    s = 0;
                }
                i = j;
            }

        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

 

H题

Description

Mathmen love mathematics, and they live on the number line. All the mathmen spend all their time on solving mathematical problems and proving theorems. Like humen beings, mathmen don't live in the same country there are n mathmen countries on difierent positions of the number line (country i lives at position xi (one point on the number line), and xi < xi + 1 for all 1 <= i <= n - 1). Mathmen in difierent countries are good at difierent areas of mathematics. For example, mathmen living in country 1 are good at geometry, while mathmen living in country 2 are experts on algebra.

Lately, all the mathmen nations have collaborated on a huge problem, which involves all areas of mathematics, and each mathmen country is responsible for solving the part they are good at. After hours of work (they are really good at mathematics, and no problem would cost them more than one day to solve), they all finish their jobs. Now, they need to collect the result in every mathmen country, and combine them together to create the solution to the huge problem. So they decide to let one mathman collect all the results and send it to one country, where all the work will be merged.

As mathmen are devoted to solving mathematical problems, their legs has become very weak after millions of years of evolution. Therefore, they have to ride mathships to travel on the number line. Their are M types of mathships, each of which has a limit of traveling distance and a cost of IQ (yes, you need to be very brave to take mathships, you would never be able to get back the lost IQ).

There are two seasons in the mathmen world Positive and Negative. Now it is Positive, so all the mathships travels on the number line from left to right. Therefore, one man from country 1 must be selected as the volunteer to collect the results in every country and send to to country n.

There is at least one mathship of each type in every country. So, after picking the results from country 1, the volunteer needs to select one mathship with a limit of traveling distance at least the distance between country 1 and country 2 (the distance is x2 - x1), and ride it from country 1 to country 2.

Meahwhile, this trip will cost him the corresponding amount of IQ. Then, he picks the result from country 2, choose another suitable mathship (the old mathship will be maintained in country 2, and can not continue to be used), and ride it to country 3, and lose some IQ. He will repeat this process, until he reaches country n.

Mathmen care about their IQ a lot, so the volunteer wants to minimize the total cost of his IQ. Could you please write program to help him select the right mathships to take?

Input

The input contains multiple test cases. The first line of the input is an integer C, indicating the number of test cases in the input.

Each test case begins with a line containing two integers, n (2 <= n <= 10000) and m (1 <= m <= 100000), which is the number of mathmen countries and the number of mathship types. The next line contains n integers, ranging from -10^9to10^9(inclusive), indicating the positions of the mathmen countries. Then, m lines follows, each containing two non-negative integers no more than 2*109, which are the limit of traveling distance and the cost of IQ.

Output

 For each test case, output one line containing the minimum cost of IQ. If the volunteer can never reach country n, output "Impossible" (without quotation marks).

Sample

Input

<span style="color:#333333"><span style="color:#333333">Copy<code>2
3 3
0 3 10
1 0
6 1
10 10
2 1
0 1000
100 0</code></span></span>

Output

<span style="color:#333333"><span style="color:#333333">Copy<code>11
Impossible</code></span></span>

 优先队列果然奇妙,先把路径长度和飞船的飞行距离降序排序,然后枚举路径长度,对于当前的路径长度,把大于飞行距离它的飞船代价加入优先队列(已经在队列里的飞行距离肯定大于它),取出队首元素就好啦

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;

struct node
{
    int len,cost;
}a[100010];

int T,n,m,j,t,x[10010];
long long ans;

bool cmp(int k,int l)
{
    return k > l;
}

bool cmp1(node k,node l)
{
    return k.len > l.len;
}

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        scanf("%d",&t);
        priority_queue<int,vector<int>,greater<int> >q;
        for(int i = 1;i < n; ++i)
        {
            int xx;
            scanf("%d",&xx);
            x[i - 1] = xx - t;
            t = xx;
        }
        n--;
        sort(x,x + n,cmp);
       // for(int i = 0;i < n; ++i) cout<<x[i]<<' ';
        for(int i = 0;i < m; ++i)
            scanf("%d%d",&a[i].len,&a[i].cost);
        sort(a,a + m,cmp1);
        j = 0;
        ans = 0;
        bool flag = 1;
        for(int i = 0;i < n; ++i)
        {
            while(j < m && a[j].len >= x[i])
            {
                q.push(a[j].cost);
                j++;
            }
            if(q.empty())
            {
                flag = 0;
                printf("Impossible\n");
                break;
            }
            else ans += q.top();\
        }
        if(flag) printf("%lld\n",ans);
    }
    return 0;
}