QOJ.ac
QOJ
ID | 提交记录ID | 题目 | Hacker | Owner | 结果 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|
#371 | #164760 | #7185. Poor Students | ucup-team1196 | ucup-team004 | Failed. | 2023-09-10 14:39:54 | 2023-09-10 14:39:54 |
詳細信息
Extra Test:
Accepted
time: 0ms
memory: 3608kb
input:
6 2 1 2 1 3 1 4 1 5 1 6 1 7 3 4
output:
12
result:
ok answer is '12'
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#164760 | #7185. Poor Students | ucup-team004# | AC ✓ | 803ms | 30936kb | C++20 | 2.4kb | 2023-09-05 13:29:11 | 2023-09-05 13:29:11 |
answer
#include <bits/stdc++.h>
using i64 = long long;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, k;
std::cin >> n >> k;
std::vector c(n, std::vector<int>(k + 1));
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
std::cin >> c[i][j];
}
c[i][k] = 1E9 + 1;
}
std::vector<int> a(k + 1);
for (int i = 0; i < k; i++) {
std::cin >> a[i];
}
i64 ans = i64(1E9 + 1) * n;
std::vector f(k + 1, std::vector(k + 1, std::set<std::pair<int, int>>{}));
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
f[j][k].insert({c[i][j] - c[i][k], i});
}
}
constexpr i64 inf = 1E18;
for (int t = 0; t < n; t++) {
std::vector<i64> d(k + 1, inf);
std::queue<int> q;
std::vector<bool> inq(k + 1);
std::vector<int> lst(k + 1, -1);
for (int i = 0; i < k; i++) {
if (a[i] > 0) {
q.push(i);
inq[i] = true;
d[i] = 0;
}
}
while (!q.empty()) {
int x = q.front();
q.pop();
inq[x] = false;
for (int y = 0; y <= k; y++) {
if (!f[x][y].empty()) {
i64 v = d[x] + f[x][y].begin()->first;
if (v < d[y]) {
d[y] = v;
lst[y] = x;
if (!inq[y]) {
inq[y] = true;
q.push(y);
}
}
}
}
}
// std::cerr << (1E9 + 1) + d[k] << "\n";
ans += d[k];
for (int i = k; lst[i] != -1; i = lst[i]) {
int j = lst[i];
int s = f[j][i].begin()->second;
for (int x = 0; x <= k; x++) {
if (x != i) {
f[x][i].erase({c[s][x] - c[s][i], s});
}
}
for (int x = 0; x <= k; x++) {
if (x != j) {
f[x][j].insert({c[s][x] - c[s][j], s});
}
}
a[j]--;
a[i]++;
}
}
std::cout << ans << "\n";
return 0;
}