제출 #217234

#제출 시각아이디문제언어결과실행 시간메모리
217234NONAMEPutovanje (COCI20_putovanje)C++17
110 / 110
164 ms44540 KiB
#include <bits/stdc++.h> #include <time.h> //#include <random> //#ifndef _LOCAL //#pragma GCC optimize("-O3") //#pragma GCC optimize("Ofast") //#pragma GCC optimize("unroll-loops") //#endif #define sz(x) int(x.size()) #define in(x) freopen(x, "r", stdin) #define out(x) freopen(x, "w", stdout) #define N 2 * 100500 #define oo ll(1e16) #define pii pair <int, int> #define pll pair <ll, ll> #define ft first #define sd second #define pb push_back #define ppb pop_back #define el '\n' #define elf endl #define base ll(1e9 + 7) #define re return #define nins 4294967295 using namespace std; typedef long long ll; typedef long double ld; const int LOG = 20; //mt19937 rnd(0); struct st { int v; ll c1, c2; st (int v, ll c1, ll c2) : v(v), c1(c1), c2(c2) {} }; ll n, tin[N], tout[N], timer, up[N][30], f[N]; ll rs, val[N]; vector <st> g[N]; void dfs(int v, int pr = 0) { tin[v] = timer++; up[v][0] = pr; for (int i = 1; i <= LOG; i++) up[v][i] = up[up[v][i - 1]][i - 1]; for (int i = 0; i < sz(g[v]); i++) { int nxt = g[v][i].v; if (nxt == pr) continue; dfs(nxt, v); } tout[v] = timer++; } bool is_anc(int v, int u) { return (tin[v] < tin[u]) && (tout[v] > tout[u]); } int lca(int v, int u) { if (is_anc(v, u)) return v; if (is_anc(u, v)) return u; for (int i = LOG; i >= 0; i--) if (!is_anc(up[v][i], u)) v = up[v][i]; return up[v][0]; } void find_res(int v, int pr = 0) { val[v] = f[v]; for (int i = 0; i < sz(g[v]); i++) { int nxt = g[v][i].v; if (nxt == pr) continue; find_res(nxt, v); rs += min(val[nxt] * g[v][i].c1, g[v][i].c2); val[v] += val[nxt]; } } void solve() { cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; ll c1, c2; cin >> v >> u >> c1 >> c2; v--; u--; g[v].pb(st(u, c1, c2)); g[u].pb(st(v, c1, c2)); } dfs(0); for (int i = 0; i < n - 1; i++) f[i]++, f[i + 1]++, f[lca(i, i + 1)] -= 2; find_res(0); cout << rs; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef _LOCAL in("input.txt"); int t = 1; // cin >> t; for (int i = 1; i <= t; i++) { cout << "Test #" << i << elf; clock_t start_time = clock(); solve(); cerr.precision(4); cerr << fixed; cerr << elf; cerr << "Time :: " << ld(clock() - start_time) / CLOCKS_PER_SEC << elf; cout << elf; } #else int t = 1; // cin >> t; while (t--) { solve(); cout << el; } #endif }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...