QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#381676 | #2429. Conquer The World | ckiseki | TL | 7106ms | 93636kb | C++20 | 3.7kb | 2024-04-07 20:08:07 | 2024-04-07 20:08:08 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
#ifdef CKISEKI
#include <experimental/iterator>
#define safe cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
void debug_(auto s, auto ...a) {
cerr << "\e[1;32m(" << s << ") = (";
int f = 0;
(..., (cerr << (f++ ? ", " : "") << a));
cerr << ")\e[0m\n";
}
void orange_(auto s, auto L, auto R) {
cerr << "\e[1;33m[ " << s << " ] = [ ";
using namespace experimental;
copy(L, R, make_ostream_joiner(cerr, ", "));
cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif
bool chmin(auto &a, auto b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename F, typename C>
struct MinCostCirculation {
struct ep { int to; F flow; C cost; };
int n; vector<int> vis; int visc;
vector<int> fa, fae; vector<vector<int>> g;
vector<ep> e; vector<C> pi;
MinCostCirculation(int n_) : n(n_), vis(n), visc(0), g(n), pi(n) {}
void add_edge(int u, int v, F fl, C cs) {
g[u].emplace_back((int)e.size());
e.emplace_back(v, fl, cs);
g[v].emplace_back((int)e.size());
e.emplace_back(u, 0, -cs);
}
C phi(int x) {
if (fa[x] == -1) return 0;
if (vis[x] == visc) return pi[x];
vis[x] = visc;
return pi[x] = phi(fa[x]) - e[fae[x]].cost;
}
int lca(int u, int v) {
for (; u != -1 || v != -1; swap(u, v)) if (u != -1) {
if (vis[u] == visc) return u;
vis[u] = visc; u = fa[u];
}
return -1;
}
void pushflow(int x, C &cost) {
int v = e[x ^ 1].to, u = e[x].to; ++visc;
if (int w = lca(u, v); w == -1) {
while (v != -1)
swap(x ^= 1, fae[v]), swap(u, fa[v]), swap(u, v);
} else {
int z = u, dir = 0; F f = e[x].flow;
vector<int> cyc = {x};
for (int d : {0, 1})
for (int i = (d ? u : v); i != w; i = fa[i]) {
cyc.push_back(fae[i] ^ d);
if (chmin(f, e[fae[i] ^ d].flow)) z = i, dir = d;
}
for (int i : cyc) {
e[i].flow -= f; e[i ^ 1].flow += f;
cost += f * e[i].cost;
}
if (dir) x ^= 1, swap(u, v);
while (u != z)
swap(x ^= 1, fae[v]), swap(u, fa[v]), swap(u, v);
}
}
void dfs(int u) {
vis[u] = visc;
for (int i : g[u])
if (int v = e[i].to; vis[v] != visc and e[i].flow)
fa[v] = u, fae[v] = i, dfs(v);
}
C simplex() {
fa.assign(g.size(), -1); fae.assign(g.size(), -1);
C cost = 0; ++visc; dfs(0);
for (int fail = 0; fail < ssize(e); )
for (int i = 0; i < ssize(e); i++)
if (e[i].flow and e[i].cost < phi(e[i ^ 1].to) - phi(e[i].to))
fail = 0, pushflow(i, cost), ++visc;
else ++fail;
return cost;
}
};
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
MinCostCirculation<int,__int128> flow(n + 2);
const int S = 0, T = n + 1;
constexpr int INF = 1'000'000;
constexpr int64_t INF64 = 1'000'000'000'000LL;
vector<int> o(n+2);
iota(all(o), 0);
shuffle(all(o), mt19937(7122));
for (int i = 1; i < n; ++i) {
int u, v, w;
cin >> u >> v >> w;
flow.add_edge(o[u], o[v], INF, w);
flow.add_edge(o[v], o[u], INF, w);
}
int demand_sum = 0;
for (int i = 1; i <= n; ++i) {
int supply, demand;
cin >> supply >> demand;
flow.add_edge(o[S], o[i], supply, 0);
flow.add_edge(o[i], o[T], demand, 0);
demand_sum += demand;
}
flow.add_edge(o[T], o[S], demand_sum, -INF64);
auto c = flow.simplex();
auto f = flow.e.back().flow;
cout << int64_t(c + f * INF64) << '\n';
return 0;
}
Details
Test #1:
score: 100
Accepted
time: 1ms
memory: 3828kb
Test #2:
score: 0
Accepted
time: 0ms
memory: 3836kb
Test #3:
score: 0
Accepted
time: 1ms
memory: 3824kb
Test #4:
score: 0
Accepted
time: 0ms
memory: 3524kb
Test #5:
score: 0
Accepted
time: 0ms
memory: 3896kb
Test #6:
score: 0
Accepted
time: 1ms
memory: 3596kb
Test #7:
score: 0
Accepted
time: 0ms
memory: 3572kb
Test #8:
score: 0
Accepted
time: 0ms
memory: 3648kb
Test #9:
score: 0
Accepted
time: 0ms
memory: 3652kb
Test #10:
score: 0
Accepted
time: 0ms
memory: 3584kb
Test #11:
score: 0
Accepted
time: 0ms
memory: 3652kb
Test #12:
score: 0
Accepted
time: 0ms
memory: 4072kb
Test #13:
score: 0
Accepted
time: 2ms
memory: 3756kb
Test #14:
score: 0
Accepted
time: 3ms
memory: 4412kb
Test #15:
score: 0
Accepted
time: 2ms
memory: 4400kb
Test #16:
score: 0
Accepted
time: 1ms
memory: 3764kb
Test #17:
score: 0
Accepted
time: 4ms
memory: 4336kb
Test #18:
score: 0
Accepted
time: 3ms
memory: 3824kb
Test #19:
score: 0
Accepted
time: 1ms
memory: 3808kb
Test #20:
score: 0
Accepted
time: 4ms
memory: 4388kb
Test #21:
score: 0
Accepted
time: 527ms
memory: 88492kb
Test #22:
score: 0
Accepted
time: 678ms
memory: 88376kb
Test #23:
score: 0
Accepted
time: 811ms
memory: 93548kb
Test #24:
score: 0
Accepted
time: 771ms
memory: 82684kb
Test #25:
score: 0
Accepted
time: 511ms
memory: 84248kb
Test #26:
score: 0
Accepted
time: 933ms
memory: 93520kb
Test #27:
score: 0
Accepted
time: 379ms
memory: 85456kb
Test #28:
score: 0
Accepted
time: 788ms
memory: 86496kb
Test #29:
score: 0
Accepted
time: 670ms
memory: 93636kb
Test #30:
score: 0
Accepted
time: 2625ms
memory: 92532kb
Test #31:
score: 0
Accepted
time: 1078ms
memory: 92524kb
Test #32:
score: 0
Accepted
time: 771ms
memory: 92444kb
Test #33:
score: 0
Accepted
time: 7106ms
memory: 92436kb
Test #34:
score: -100
Time Limit Exceeded