QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#173527 | #7185. Poor Students | ucup-team133# | TL | 2ms | 3868kb | C++23 | 2.8kb | 2023-09-09 23:57:54 | 2023-09-09 23:57:54 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
vector c(n, vector<int>(k + 2));
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
cin >> c[i][j];
}
}
vector<int> a(k);
for (int i = 0; i < k; i++) {
cin >> a[i];
}
long long ans = 0;
multiset<pair<int, int>> g[12][12] = {};
for (int i = 0; i < k; i++) {
for (int j = 0; j < a[i]; j++) {
g[i][k].emplace(0, -1);
}
}
const long long inf = 1e18;
auto Flow = [&](int s) {
vector<vector<long long>> d(k + 2, vector<long long>(k + 2, inf));
for (int i = 0; i < k + 2; i++) {
d[i][i] = 0;
}
for (int i = 0; i < k + 2; i++) {
for (int j = 0; j < k + 2; j++) {
if (i != j && !g[i][j].empty()) {
d[i][j] = g[i][j].begin()->first;
}
}
}
for (int z = 0; z < k + 2; z++) {
for (int x = 0; x < k + 2; x++) {
for (int y = 0; y < k + 2; y++) {
d[x][y] = min(d[x][y], d[x][z] + d[z][y]);
}
}
}
ans += d[s][k];
int t = k;
set<int> st;
for (int i = 0; i < k; i++) {
st.emplace(i);
}
st.emplace(k + 1);
while (s != t) {
for (int i : st) {
if (!g[i][t].empty() && d[s][t] == d[s][i] + g[i][t].begin()->first) {
auto p = *g[i][t].begin();
int z = p.second;
if (z == -1) {
g[i][t].erase(g[i][t].begin());
g[t][i].emplace(0, -1);
} else {
for (int j = 0; j < k; j++) {
if (i != j) {
g[i][j].erase(make_pair(c[z][j] - c[z][i], z));
}
if (t != j) {
g[t][j].emplace(make_pair(c[z][j] - c[z][t], z));
}
}
}
t = i;
st.erase(t);
break;
}
}
}
};
for (int i = 0; i < n; i++) {
int s = (int) (min_element(c[i].begin(), c[i].end()) - c[i].begin());
for (int x = 0; x < k; x++) {
if (x != s) {
g[s][x].emplace(c[i][x] - c[i][s], i);
}
g[k + 1][x].emplace(c[i][x], i);
}
ans += c[i][s];
}
for (int i = 0; i < n; i++) {
Flow(k + 1);
}
cout << ans << '\n';
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 3868kb
input:
6 2 1 2 1 3 1 4 1 5 1 6 1 7 3 4
output:
12
result:
ok answer is '12'
Test #2:
score: 0
Accepted
time: 1ms
memory: 3636kb
input:
3 3 1 2 3 2 4 6 6 5 4 1 1 1
output:
8
result:
ok answer is '8'
Test #3:
score: -100
Time Limit Exceeded
input:
1000 10 734 303 991 681 755 155 300 483 702 442 237 256 299 675 671 757 112 853 759 233 979 340 288 377 718 199 935 666 576 842 537 363 592 349 494 961 864 727 84 813 340 78 600 492 118 421 478 925 552 617 517 589 716 7 928 638 258 297 706 787 266 746 913 978 436 859 701 951 137 44 815 336 471 720 2...