#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define N 352
/*
    重量*单价+重量*距离 = 重量*(距离+单价) 预处理单价 
    贪心:优先买价格低的
*/
struct Node {
    int p;// p = (单价+距离) 
    int w;
}c[N];

bool cmp(Node a, Node b)
{
    return a.p != b.p ? a.p < b.p : a.w > b.w;
}

int main()
{
    freopen("d:\\in.txt", "r", stdin);
    int t;
    int K, E, n; 
    scanf("%d", &t);
    while(t--) {
        scanf("%d%d%d", &K, &E, &n);
        int a, b;
        for(int i=0; i<n; i++){
            scanf("%d%d%d", &a, &c[i].w, &b);
            c[i].p = E-a+b; 
        } 
        sort(c, c+n, cmp);
        int res = 0, totw = 0;
        for(int i=0; i<n; i++){
            if(totw >= K) break;
            if(totw+c[i].w > K)
                res += c[i].p*(K-totw);
            else
                res += c[i].p*c[i].w;
            totw += c[i].w;
        }
        printf("%d\n", res);
    } 
    return 0;
}
 posted on 2015-04-01 21:05  平和之心  阅读(101)  评论(0编辑  收藏  举报