比赛 期末考试1 评测结果 AEWWAWWWWW
题目名称 Constructive 最终得分 20
用户昵称 赵飞羽 运行时间 6.099 s
代码语言 C++ 内存使用 3.66 MiB
提交时间 2026-02-08 10:34:06
显示代码纯文本
#include <bits/stdc++.h>
#define int long long
#define MAX_TIME 0.99
using namespace std;

const int N = 100;
int n, x, y, ans = 9e18, res, w[N];
struct node{
    int l, r, v;
} a[N];

void solve1() {
    ans = y * a[1].v + x * a[2].v;
    return;
}

void solve2() {
    int p = 1;
    double cnt = 0.0, minn = 9e18;
    for (int i = 1; i <= n; i++) {
        if (a[i].l == 0) continue;
        cnt = (double)a[i].v / (double)a[i].l;
        if (cnt < minn) {
            minn = cnt;
            p = i;
        }
    }
    ans = x / p * a[p].v + (x - x / p * p) * a[x-x/p*p].v;
    return;
}

void ran() {
	srand(time(0));
	srand(rand());
	srand(rand());
	return;
}

void solve3() {
    ran();
    while ((double)clock() / CLOCKS_PER_SEC < MAX_TIME) {
        int tot = 0, xx = 0, yy = 0;
        for (int i = 1; i <= n; i++) {
            if (a[i].l == 0 && a[i].r == 0) continue;
            else if (a[i].l == 0) w[i] = rand() % (y / a[i].r);
            else if (a[i].r == 0) w[i] = rand() % (x / a[i].l);
            else w[i] = rand() % min(x / a[i].l, y / a[i].r);
        }
        for (int i = 1; i <= n; i++) {
            xx += w[i] * a[i].l;
            yy += w[i] * a[i].r;
            if (xx > x || yy > y) break;
        }
        if (xx == x && yy == y) {
            for (int i = 1; i <= n; i++) {
                tot += w[i] * a[i].v;
                if (tot > ans) break;
            }
            ans = min(tot, ans);
        }
    }
    return;
}

signed main() {
    freopen("tioj_constructive.in", "r", stdin);
    freopen("tioj_constructive.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
	cin >> n >> x >> y;
	for (int i = 1; i <= n; i++) {
	    cin >> a[i].l >> a[i].r >> a[i].v;
	    if (a[i].l == a[i].r) res++;
    }
	if (n == 2 && a[1].l == 0 && a[1].r == 1 && a[2].l == 1 && a[2].r == 0) solve1();
	else if (res == n && x == y) solve2();
	else solve3();
	if (ans == 9e18) ans = -1;
	cout << ans;
    return 0;
}