QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#288354 | #7883. Takeout Delivering | ckiseki# | ML | 3ms | 77728kb | C++20 | 3.4kb | 2023-12-22 15:45:20 | 2023-12-22 15:45:20 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
constexpr int N = 300000 + 5;
constexpr int K = 18;
#define all(x) begin(x), end(x)
#ifdef CKISEKI
#include <experimental/iterator>
#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
struct Dsu {
vector<int> pa, rk;
Dsu(int n) : pa(n), rk(n) {
iota(all(pa), 0);
}
int anc(int x) {
return x==pa[x] ? x : pa[x]=anc(pa[x]);
}
bool join(int x, int y) {
x = anc(x);
y = anc(y);
if (x == y) return false;
if (rk[x] < rk[y]) swap(x, y);
if (rk[x] == rk[y]) ++rk[x];
pa[y] = x;
return true;
}
};
vector<pair<int, int>> g[N];
struct D {
int s;
array<int, 2> v;
D() : s{0}, v{} {}
void push_back(int x) {
v[s++] = x;
}
auto begin() { return v.begin(); }
auto end() { return v.begin() + s; }
D operator+(const D &rhs) const {
vector<int> ret;
for (int i = 0; i < s; ++i)
ret.push_back(v[i]);
for (int i = 0; i < rhs.s; ++i)
ret.push_back(rhs.v[i]);
ranges::sort(ret, greater<>());
D res;
for (int i = 0; i < 2; ++i) {
if (i == ssize(ret))
break;
res.push_back(ret[i]);
}
return res;
}
};
D val[N][K];
int pa[N][K];
int tin[N], tout[N], tc;
void dfs(int u, int f) {
tin[u] = tc++;
for (int i = 1; i < K; ++i) {
val[u][i] = val[u][i - 1] + val[pa[u][i - 1]][i - 1];
pa[u][i] = pa[pa[u][i - 1]][i - 1];
}
for (auto [v, w] : g[u]) {
if (v != f) {
val[v][0].push_back(w);
pa[v][0] = u;
dfs(v, u);
}
}
tout[u] = tc;
}
bool anc(int u, int v) {
return tin[u] <= tin[v] and tout[v] <= tout[u];
}
int lca(int u, int v) {
if (anc(u, v))
return u;
for (int i = K - 1; i >= 0; --i)
if (!anc(pa[u][i], v))
u = pa[u][i];
return pa[u][0];
}
D up(int u, int f) {
assert(anc(f, u));
if (u == f)
return {};
D ret = {};
for (int i = K - 1; i >= 0; --i) {
if (!anc(pa[u][i], f)) {
ret = ret + val[u][i];
u = pa[u][i];
}
}
return ret + val[u][0];
}
D query(int u, int v) {
int o = lca(u, v);
return up(u, o) + up(v, o);
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, m;
cin >> n >> m;
int ans = 2e9;
vector<tuple<int,int,int>> e;
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
--u, --v;
e.emplace_back(w, u, v);
tie(u, v) = minmax(u, v);
if (u == 0 && v == n - 1)
ans = min(ans, w);
}
ranges::sort(e);
Dsu dsu(n);
for (auto [w, u, v] : e) {
if (dsu.join(u, v)) {
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
}
dfs(0, 0);
for (auto [w, u, v] : e) {
for (int s : {0, n - 1}) {
const int t = n - 1 - s;
vector<int> ws = {w};
for (auto wi : query(u, s))
ws.push_back(wi);
for (auto wi : query(v, t))
ws.push_back(wi);
ranges::sort(ws, greater<>());
if (ws.size() == 1) {
ans = min(ans, ws[0]);
} else {
ans = min(ans, ws[0] + ws[1]);
}
}
}
cout << ans << "\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 77728kb
input:
4 6 1 2 2 1 3 4 1 4 7 2 3 1 2 4 3 3 4 9
output:
5
result:
ok 1 number(s): "5"
Test #2:
score: -100
Memory Limit Exceeded
input:
300000 299999 80516 80517 597830404 110190 110191 82173886 218008 218009 954561262 250110 250111 942489774 66540 66541 156425292 34947 34948 239499776 273789 273790 453201232 84428 84429 439418398 98599 98600 326095035 55636 55637 355015760 158611 158612 684292473 43331 43332 43265001 171621 171622 ...