QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#391936 | #7185. Poor Students | KKT89 | WA | 44ms | 6060kb | C++17 | 6.1kb | 2024-04-16 22:12:17 | 2024-04-16 22:12:17 |
Judging History
answer
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) { return (ull)rng() % B; }
template <typename flow_t = int, typename cost_t = int> struct mcSFlow {
struct Edge {
cost_t c;
flow_t f;
int to, rev;
Edge(int _to, cost_t _c, flow_t _f, int _rev) : c(_c), f(_f), to(_to), rev(_rev) {}
};
static constexpr cost_t INFCOST = numeric_limits<cost_t>::max() / 2;
cost_t eps;
int N, S, T;
vector<vector<Edge>> G;
vector<unsigned int> isq, cur;
vector<flow_t> ex;
vector<cost_t> h;
mcSFlow(int _N, int _S, int _T) : eps(0), N(_N), S(_S), T(_T), G(_N) {}
void add_edge(int a, int b, cost_t cost, flow_t cap) {
assert(cap >= 0);
assert(a >= 0 && a < N && b >= 0 && b < N);
if (a == b) {
assert(cost >= 0);
return;
}
cost *= N;
eps = max(eps, abs(cost));
G[a].emplace_back(b, cost, cap, G[b].size());
G[b].emplace_back(a, -cost, 0, G[a].size() - 1);
}
void add_flow(Edge &e, flow_t f) {
Edge &back = G[e.to][e.rev];
if (!ex[e.to] && f) hs[h[e.to]].push_back(e.to);
e.f -= f;
ex[e.to] += f;
back.f += f;
ex[back.to] -= f;
}
vector<vector<int>> hs;
vector<int> co;
flow_t max_flow() {
ex.assign(N, 0);
h.assign(N, 0);
hs.resize(2 * N);
co.assign(2 * N, 0);
cur.assign(N, 0);
h[S] = N;
ex[T] = 1;
co[0] = N - 1;
for (auto &e : G[S])
add_flow(e, e.f);
if (hs[0].size())
for (int hi = 0; hi >= 0;) {
int u = hs[hi].back();
hs[hi].pop_back();
while (ex[u] > 0) { // discharge u
if (cur[u] == G[u].size()) {
h[u] = 1e9;
for (unsigned int i = 0; i < G[u].size(); ++i) {
auto &e = G[u][i];
if (e.f && h[u] > h[e.to] + 1) {
h[u] = h[e.to] + 1, cur[u] = i;
}
}
if (++co[h[u]], !--co[hi] && hi < N)
for (int i = 0; i < N; ++i)
if (hi < h[i] && h[i] < N) {
--co[h[i]];
h[i] = N + 1;
}
hi = h[u];
} else if (G[u][cur[u]].f && h[u] == h[G[u][cur[u]].to] + 1) add_flow(G[u][cur[u]], min(ex[u], G[u][cur[u]].f));
else ++cur[u];
}
while (hi >= 0 && hs[hi].empty())
--hi;
}
return -ex[S];
}
void push(Edge &e, flow_t amt) {
if (e.f < amt) amt = e.f;
e.f -= amt;
ex[e.to] += amt;
G[e.to][e.rev].f += amt;
ex[G[e.to][e.rev].to] -= amt;
}
void relabel(int vertex) {
cost_t newHeight = -INFCOST;
for (unsigned int i = 0; i < G[vertex].size(); ++i) {
Edge const &e = G[vertex][i];
if (e.f && newHeight < h[e.to] - e.c) {
newHeight = h[e.to] - e.c;
cur[vertex] = i;
}
}
h[vertex] = newHeight - eps;
}
static constexpr int scale = 2;
pair<flow_t, cost_t> minCostMaxFlow() {
cost_t retCost = 0;
for (int i = 0; i < N; ++i)
for (Edge &e : G[i])
retCost += e.c * (e.f);
// find max-flow
flow_t retFlow = max_flow();
h.assign(N, 0);
ex.assign(N, 0);
isq.assign(N, 0);
cur.assign(N, 0);
queue<int> q;
for (; eps; eps >>= scale) {
// refine
fill(cur.begin(), cur.end(), 0);
for (int i = 0; i < N; ++i)
for (auto &e : G[i])
if (h[i] + e.c - h[e.to] < 0 && e.f) push(e, e.f);
for (int i = 0; i < N; ++i) {
if (ex[i] > 0) {
q.push(i);
isq[i] = 1;
}
}
// make flow feasible
while (!q.empty()) {
int u = q.front();
q.pop();
isq[u] = 0;
while (ex[u] > 0) {
if (cur[u] == G[u].size()) relabel(u);
for (unsigned int &i = cur[u], max_i = G[u].size(); i < max_i; ++i) {
Edge &e = G[u][i];
if (h[u] + e.c - h[e.to] < 0) {
push(e, ex[u]);
if (ex[e.to] > 0 && isq[e.to] == 0) {
q.push(e.to);
isq[e.to] = 1;
}
if (ex[u] == 0) break;
}
}
}
}
if (eps > 1 && eps >> scale == 0) {
eps = 1 << scale;
}
}
for (int i = 0; i < N; ++i) {
for (Edge &e : G[i]) {
retCost -= e.c * (e.f);
}
}
return make_pair(retFlow, retCost / 2 / N);
}
flow_t getFlow(Edge const &e) { return G[e.to][e.rev].f; }
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
int S = n + k, T = S + 1;
mcSFlow g(n + k + 2, S, T);
for (int i = 0; i < n; ++i) {
g.add_edge(S, i, 0, 1);
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < k; ++j) {
int c;
cin >> c;
g.add_edge(i, j + n, c, 1);
}
}
for (int i = 0; i < k; ++i) {
int a;
cin >> a;
g.add_edge(i + n, T, 0, a);
}
cout << g.minCostMaxFlow().second << endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3632kb
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: 3628kb
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: 0
Accepted
time: 6ms
memory: 3988kb
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...
output:
92039
result:
ok answer is '92039'
Test #4:
score: -100
Wrong Answer
time: 44ms
memory: 6060kb
input:
5000 10 14 114 254 832 38 904 25 147 998 785 917 694 750 372 379 887 247 817 999 117 802 15 799 515 316 42 69 247 95 144 727 398 509 725 682 456 369 656 693 955 923 1 681 631 962 826 233 963 289 856 165 491 488 832 111 950 853 791 929 240 509 843 667 970 469 260 447 477 161 431 514 903 627 236 144 3...
output:
33409
result:
wrong answer expected '461878', found '33409'