Submission #655265

#TimeUsernameProblemLanguageResultExecution timeMemory
655265happypotatoDreaming (IOI13_dreaming)C++17
100 / 100
118 ms16416 KiB
#include "dreaming.h" #include <bits/stdc++.h> using namespace std; #define pii pair<int, int> #define ff first #define ss second #define pb push_back const int mxN = 1e5 + 1; vector<pii> adj[mxN]; bool vis[mxN]; int dist[mxN], dist2[mxN]; pii dfs(int u, int par = -1) { vis[u] = true; pii res = {u, 0}; for (pii &v : adj[u]) { if (v.ff != par) { pii ret = dfs(v.ff, u); ret.ss += v.ss; if (ret.ss > res.ss) { res = ret; } } } return res; } int FindRadii(int lhs, int rhs) { if (lhs == rhs) return 0; priority_queue<pii, vector<pii>, greater<pii>> pq; vector<int> nodes; // from lhs pq.push({lhs, 0}); dist[lhs] = 0; nodes.pb(lhs); while (!pq.empty()) { pii cur = pq.top(); pq.pop(); if (cur.ss > dist[cur.ff]) continue; nodes.pb(cur.ff); for (pii &v : adj[cur.ff]) { if (dist[v.ff] > cur.ss + v.ss) { dist[v.ff] = cur.ss + v.ss; pq.push({v.ff, dist[v.ff]}); } } } // from rhs pq.push({rhs, 0}); dist2[rhs] = 0; while (!pq.empty()) { pii cur = pq.top(); pq.pop(); if (cur.ss > dist2[cur.ff]) continue; for (pii &v : adj[cur.ff]) { if (dist2[v.ff] > cur.ss + v.ss) { dist2[v.ff] = cur.ss + v.ss; pq.push({v.ff, dist2[v.ff]}); } } } int ans = 2e9; for (int &x : nodes) { ans = min(ans, max(dist[x], dist2[x])); } return ans; } int travelTime(int n, int m, int L, int A[], int B[], int T[]) { for (int i = 0; i < m; i++) { A[i]++; B[i]++; adj[A[i]].pb({B[i], T[i]}); adj[B[i]].pb({A[i], T[i]}); } for (int i = 1; i <= n; i++) { dist[i] = dist2[i] = 2e9; } vector<int> fin; int ans = 0; for (int i = 1; i <= n; i++) { if (!vis[i]) { pii lhs = dfs(i); pii rhs = dfs(lhs.ff); ans = max(ans, rhs.ss); fin.pb(FindRadii(lhs.ff, rhs.ff)); } } sort(fin.begin(), fin.end(), greater<int>()); if (fin.size() == 1) ans = max(ans, fin[0]); else if (fin.size() == 2) ans = max(ans, fin[0] + fin[1] + L); else ans = max(ans, max(fin[0] + fin[1] + L, fin[1] + fin[2] + L * 2)); return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...