QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#445885 | #7185. Poor Students | CelestialCoder# | TL | 0ms | 3828kb | C++20 | 4.6kb | 2024-06-16 16:37:24 | 2024-06-16 16:37:24 |
Judging History
answer
#include <bits/stdc++.h>
#ifdef SHARAELONG
#include "../../cpp-header/debug.hpp"
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
struct CustomPQ {
priority_queue<pii> pq, rm;
void push(pii x) {
pq.push({ -x.first, x.second });
}
void erase(pii x) {
rm.push({ -x.first, x.second });
}
pii top() {
while (!rm.empty() && pq.top() == rm.top()) {
pq.pop();
rm.pop();
}
return { -pq.top().first, pq.top().second };
}
void pop() {
while (!rm.empty() && pq.top() == rm.top()) {
pq.pop();
rm.pop();
}
pq.pop();
}
bool empty() {
while (!rm.empty() && pq.top() == rm.top()) {
pq.pop();
rm.pop();
}
return pq.empty();
}
};
const ll INF = 1e18;
const int MAX_K = 10;
ll adj[MAX_K][MAX_K];
int aidx[MAX_K][MAX_K];
void solve() {
int n, k;
cin >> n >> k;
vector<vector<int>> c(n, vector<int>(k));
for (int i=0; i<n; ++i) {
for (int j=0; j<k; ++j) cin >> c[i][j];
}
vector<int> a(k);
for (int& x: a) cin >> x;
vector<int> assign(n, -1);
vector<int> assign_sz(k, 0);
vector<vector<CustomPQ>> edge_pq(k, vector<CustomPQ>(k));
vector<CustomPQ> rem_pq(k);
for (int i=0; i<k; ++i) {
for (int j=0; j<n; ++j) {
rem_pq[i].push({ c[j][i], j });
}
}
ll ans = 0;
for (int i=0; i<n; ++i) {
// cout << "i = " << i << endl;
ll cost = INF;
int last = -1;
vector<int> path;
for (int i=0; i<MAX_K; ++i) {
for (int j=0; j<MAX_K; ++j) {
adj[i][j] = (i == j ? 0ll : INF);
aidx[i][j] = -1;
}
}
for (int i=0; i<k; ++i) {
for (int j=0; j<k; ++j) {
if (!edge_pq[i][j].empty()) {
auto res = edge_pq[i][j].top();
adj[i][j] = res.first;
aidx[i][j] = res.second;
}
}
}
for (int l=0; l<k; ++l) {
for (int i=0; i<k; ++i) {
for (int j=0; j<k; ++j) {
adj[i][j] = min(adj[i][j], adj[i][l] + adj[l][j]);
}
}
}
for (int j=0; j<k; ++j) {
// cout << "j = " << j << endl;
auto fi_cost = rem_pq[j].top();
if (assign_sz[j] < a[j]) {
if (cost > fi_cost.first) {
cost = fi_cost.first;
path.clear();
path.push_back(fi_cost.second);
last = j;
}
} else {
// j -> l
for (int l=0; l<k; ++l) {
if (assign_sz[l] < a[l]) {
if (cost > adj[j][l] + fi_cost.first) {
cost = adj[j][l] + fi_cost.first;
path.clear();
path.push_back(fi_cost.second);
int cur = j;
while (cur != l) {
for (int m=0; m<k; ++m) {
if (m != cur && adj[cur][l] == adj[cur][m] + adj[m][l]) {
path.push_back(aidx[cur][m]);
cur = m;
break;
}
}
}
last = l;
}
}
}
}
}
ans += cost;
// print(cost, path, last);
for (int j=0; j<path.size(); ++j) {
int x = path[j];
if (j > 0) {
for (int l=0; l<k; ++l) {
edge_pq[assign[x]][l].erase({ c[x][l]-c[x][assign[x]], x });
}
assign_sz[assign[x]]--;
}
assign[x] = (j+1 == path.size() ? last : assign[path[j+1]]);
assign_sz[assign[x]]++;
for (int l=0; l<k; ++l) {
edge_pq[assign[x]][l].push({ c[x][l]-c[x][assign[x]], x });
}
}
for (int j=0; j<k; ++j) {
rem_pq[j].erase({ c[path[0]][j], path[0] });
}
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3828kb
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: 0ms
memory: 3596kb
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...