QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#520162 | #6455. Fancy Antiques | Andycipation | TL | 0ms | 3800kb | C++20 | 2.0kb | 2024-08-15 11:15:17 | 2024-08-15 11:15:18 |
Judging History
answer
/*
* author: ADMathNoob
* created: 08/14/24 22:28:48
* problem: https://qoj.ac/problem/6455
*/
/*
Comments about problem:
*/
#include <bits/stdc++.h>
using namespace std;
#ifdef _DEBUG
#include "debug.h"
#else
#define debug(...) 42
#endif
const int inf = 1e9 + 5;
const int N = 100;
const int M = 40;
int n, m, k;
int x[N], y[N], px[N], py[N];
vector<int> g[M];
bool has_self[M];
int state[M];
int GetCost(int id) {
int ret = inf;
if (state[x[id]] == 1) ret = min(ret, px[id]);
if (state[y[id]] == 1) ret = min(ret, py[id]);
return ret;
}
int Solve(int v, int used, int cur) {
assert(used <= k);
if (v == m) {
return cur;
}
auto GetAddedCost = [&] {
int add = 0;
for (int id : g[v]) {
int u = x[id] ^ y[id] ^ v;
if (u <= v) {
add += GetCost(id);
}
}
return add;
};
if (state[v] != -1) {
return Solve(v + 1, used, cur + GetAddedCost());
}
int res = inf;
if (!has_self[v]) {
state[v] = 0;
vector<int> changed;
for (int id : g[v]) {
int u = x[id] ^ y[id] ^ v;
assert(state[u] != 0);
if (state[u] == -1) {
state[u] = 1;
changed.push_back(u);
}
}
int new_used = used + changed.size();
if (new_used <= k) {
res = min(res, Solve(v + 1, new_used, cur + GetAddedCost()));
}
for (int u : changed) {
state[u] = -1;
}
}
if (used < k) {
state[v] = 1;
res = min(res, Solve(v + 1, used + 1, cur + GetAddedCost()));
}
state[v] = -1;
return res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> k;
for (int i = 0; i < n; i++) {
cin >> x[i] >> px[i] >> y[i] >> py[i];
--x[i]; --y[i];
g[x[i]].push_back(i);
if (x[i] == y[i]) {
has_self[i] = true;
} else {
g[y[i]].push_back(i);
}
}
fill(state, state + m, -1);
int ans = Solve(0, 0, 0);
cout << (ans < inf ? ans : -1) << '\n';
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3612kb
input:
3 3 2 1 30 2 50 2 70 3 10 3 20 1 80
output:
60
result:
ok single line: '60'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3800kb
input:
3 3 1 1 30 2 50 2 70 3 10 3 20 1 80
output:
-1
result:
ok single line: '-1'
Test #3:
score: -100
Time Limit Exceeded
input:
3 40 20 1 30 2 50 2 70 3 10 3 20 1 80