QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#381673 | #2429. Conquer The World | ckiseki | TL | 2097ms | 92664kb | C++20 | 3.6kb | 2024-04-07 20:03:54 | 2024-04-07 20:03:54 |
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;
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 << int64_t(c + f * INF64) << '\n';
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3632kb
Test #2:
score: 0
Accepted
time: 0ms
memory: 3880kb
Test #3:
score: 0
Accepted
time: 0ms
memory: 3584kb
Test #4:
score: 0
Accepted
time: 1ms
memory: 3648kb
Test #5:
score: 0
Accepted
time: 0ms
memory: 3816kb
Test #6:
score: 0
Accepted
time: 0ms
memory: 3584kb
Test #7:
score: 0
Accepted
time: 0ms
memory: 3576kb
Test #8:
score: 0
Accepted
time: 0ms
memory: 3588kb
Test #9:
score: 0
Accepted
time: 0ms
memory: 3636kb
Test #10:
score: 0
Accepted
time: 1ms
memory: 3820kb
Test #11:
score: 0
Accepted
time: 1ms
memory: 3648kb
Test #12:
score: 0
Accepted
time: 2ms
memory: 3876kb
Test #13:
score: 0
Accepted
time: 1ms
memory: 3772kb
Test #14:
score: 0
Accepted
time: 3ms
memory: 4408kb
Test #15:
score: 0
Accepted
time: 0ms
memory: 4308kb
Test #16:
score: 0
Accepted
time: 0ms
memory: 3764kb
Test #17:
score: 0
Accepted
time: 4ms
memory: 4332kb
Test #18:
score: 0
Accepted
time: 3ms
memory: 4000kb
Test #19:
score: 0
Accepted
time: 1ms
memory: 3748kb
Test #20:
score: 0
Accepted
time: 0ms
memory: 4332kb
Test #21:
score: 0
Accepted
time: 508ms
memory: 87700kb
Test #22:
score: 0
Accepted
time: 536ms
memory: 87548kb
Test #23:
score: 0
Accepted
time: 582ms
memory: 92608kb
Test #24:
score: 0
Accepted
time: 813ms
memory: 82084kb
Test #25:
score: 0
Accepted
time: 428ms
memory: 83588kb
Test #26:
score: 0
Accepted
time: 1089ms
memory: 92664kb
Test #27:
score: 0
Accepted
time: 389ms
memory: 84680kb
Test #28:
score: 0
Accepted
time: 832ms
memory: 85636kb
Test #29:
score: 0
Accepted
time: 671ms
memory: 92540kb
Test #30:
score: 0
Accepted
time: 2097ms
memory: 91512kb
Test #31:
score: 0
Accepted
time: 1106ms
memory: 91548kb
Test #32:
score: 0
Accepted
time: 712ms
memory: 91608kb
Test #33:
score: -100
Time Limit Exceeded