QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#381519 | #2429. Conquer The World | ckiseki | TL | 7565ms | 59504kb | C++20 | 3.5kb | 2024-04-07 18:30:27 | 2024-04-07 18:30:28 |
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, int64_t> flow(n + 2);
const int S = 0, T = n + 1;
constexpr int INF = 1'000'000;
constexpr int64_t INF64 = 1'000'000'000LL;
for (int i = 1; i < n; ++i) {
int u, v, w;
cin >> u >> v >> w;
flow.add_edge(u, v, INF, w);
flow.add_edge(v, u, INF, w);
}
int demand_sum = 0;
for (int i = 1; i <= n; ++i) {
int supply, demand;
cin >> supply >> demand;
flow.add_edge(S, i, supply, 0);
flow.add_edge(i, T, demand, 0);
demand_sum += demand;
}
flow.add_edge(T, S, demand_sum, -INF64);
auto c = flow.simplex();
auto f = flow.e.back().flow;
cout << c + f * INF64 << '\n';
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3580kb
Test #2:
score: 0
Accepted
time: 0ms
memory: 3568kb
Test #3:
score: 0
Accepted
time: 1ms
memory: 3832kb
Test #4:
score: 0
Accepted
time: 1ms
memory: 3672kb
Test #5:
score: 0
Accepted
time: 0ms
memory: 3636kb
Test #6:
score: 0
Accepted
time: 0ms
memory: 3580kb
Test #7:
score: 0
Accepted
time: 1ms
memory: 3516kb
Test #8:
score: 0
Accepted
time: 1ms
memory: 3592kb
Test #9:
score: 0
Accepted
time: 1ms
memory: 3676kb
Test #10:
score: 0
Accepted
time: 1ms
memory: 3592kb
Test #11:
score: 0
Accepted
time: 0ms
memory: 3648kb
Test #12:
score: 0
Accepted
time: 2ms
memory: 3808kb
Test #13:
score: 0
Accepted
time: 1ms
memory: 3756kb
Test #14:
score: 0
Accepted
time: 3ms
memory: 3848kb
Test #15:
score: 0
Accepted
time: 5ms
memory: 3824kb
Test #16:
score: 0
Accepted
time: 1ms
memory: 3960kb
Test #17:
score: 0
Accepted
time: 3ms
memory: 3908kb
Test #18:
score: 0
Accepted
time: 3ms
memory: 4060kb
Test #19:
score: 0
Accepted
time: 1ms
memory: 3780kb
Test #20:
score: 0
Accepted
time: 4ms
memory: 3932kb
Test #21:
score: 0
Accepted
time: 450ms
memory: 53496kb
Test #22:
score: 0
Accepted
time: 465ms
memory: 52724kb
Test #23:
score: 0
Accepted
time: 518ms
memory: 59504kb
Test #24:
score: 0
Accepted
time: 749ms
memory: 48024kb
Test #25:
score: 0
Accepted
time: 390ms
memory: 49588kb
Test #26:
score: 0
Accepted
time: 994ms
memory: 59504kb
Test #27:
score: 0
Accepted
time: 378ms
memory: 50580kb
Test #28:
score: 0
Accepted
time: 772ms
memory: 51472kb
Test #29:
score: 0
Accepted
time: 670ms
memory: 59408kb
Test #30:
score: 0
Accepted
time: 2081ms
memory: 58168kb
Test #31:
score: 0
Accepted
time: 1027ms
memory: 58068kb
Test #32:
score: 0
Accepted
time: 680ms
memory: 58044kb
Test #33:
score: 0
Accepted
time: 7565ms
memory: 58220kb
Test #34:
score: -100
Time Limit Exceeded