Submission #367409

#TimeUsernameProblemLanguageResultExecution timeMemory
367409arujbansalRelay Marathon (NOI20_relaymarathon)C++17
100 / 100
2382 ms164300 KiB
#include <iostream> #include <algorithm> #include <vector> #include <map> #include <set> #include <array> #include <stack> #include <queue> #include <random> #include <numeric> #include <functional> #include <chrono> #include <utility> #include <iomanip> #include <assert.h> using namespace std; void dbg_out() { cerr << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #define rng_init mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) #define rng_seed(x) mt19937 rng(x) #define all(x) (x).begin(), (x).end() #define sz(x) (int) (x).size() // #define int long long const int MXN = 1e5 + 5, INF = 1e9 + 5; int N, M, K; int par[MXN]; vector<pair<int, int>> g[MXN]; vector<array<int, 3>> edges; vector<int> dijkstra(const vector<int> &src) { priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq; vector<int> dist(N, INF); for (int i = 0; i < N; i++) par[i] = i; for (const auto &x : src) { dist[x] = 0; pq.emplace(0, x); } while (!pq.empty()) { auto cur = pq.top(); pq.pop(); int u = cur.second, cur_dist = cur.first; if (cur_dist != dist[u]) continue; for (const auto &[v, wt] : g[u]) { int new_dist = cur_dist + wt; if (new_dist < dist[v]) { dist[v] = new_dist; pq.emplace(new_dist, v); par[v] = par[u]; } } } return dist; } array<int, 3> best_pair(const vector<int> &dist) { pair<int, int> points = {-1, -1}; int points_dist = INF; for (const auto &[u, v, w] : edges) { if (par[u] == par[v]) continue; int cur_dist = dist[u] + dist[v] + w; if (cur_dist < points_dist) { points.first = par[u]; points.second = par[v]; points_dist = cur_dist; } } return array<int, 3>{points.first, points.second, points_dist}; } void solve() { cin >> N >> M >> K; for (int i = 0; i < M; i++) { int u, v, w; cin >> u >> v >> w; u--, v--; edges.push_back({u, v, w}); g[u].emplace_back(v, w); g[v].emplace_back(u, w); } vector<int> special(K); for (auto &x : special) { cin >> x; x--; } vector<int> dist_special = dijkstra(special); array<int, 3> points1 = best_pair(dist_special); vector<int> special_new; for (const auto &x : special) { if (x == points1[0] || x == points1[1]) continue; special_new.push_back(x); } vector<int> dist_special_new = dijkstra(special_new); array<int, 3> points2 = best_pair(dist_special_new); int ans = points1[2] + points2[2]; vector<int> dist1 = dijkstra(vector<int>{points1[0]}); vector<int> dist2 = dijkstra(vector<int>{points1[1]}); set<pair<int, int>> dist2_set; for (const auto &i : special) { if (i == points1[0] || i == points1[1]) continue; dist2_set.insert({dist2[i], i}); } for (const auto &i : special) { if (i == points1[0] || i == points1[1]) continue; dist2_set.erase({dist2[i], i}); pair<int, int> dist2_best = *dist2_set.begin(); dist2_set.insert({dist2[i], i}); ans = min(ans, dist1[i] + dist2_best.first); } cout << ans; } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int TC = 1; // cin >> TC; while (TC--) solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...