結果
問題 |
No.3113 The farthest point
|
ユーザー |
![]() |
提出日時 | 2025-04-20 01:55:55 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 280 ms / 2,000 ms |
コード長 | 1,566 bytes |
コンパイル時間 | 3,382 ms |
コンパイル使用メモリ | 292,800 KB |
実行使用メモリ | 19,592 KB |
最終ジャッジ日時 | 2025-04-20 01:56:05 |
合計ジャッジ時間 | 9,104 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 33 |
ソースコード
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll LINF = 4321001001001001001; struct graph { struct edge { int to; ll cost; }; vector<vector<edge>> g; vector<ll> dist; int n; graph(int n_) : g(n_), dist(n_, LINF), n(n_) {} void addEdge(int from, int to, ll cost) { g[from].push_back(edge{to, cost}); } void addEdgeBoth(int n1, int n2, ll cost) { addEdge(n1, n2, cost); addEdge(n2, n1, cost); } void dijkstra(int st) { for (int i = 0; i < n; i++) dist[i] = LINF; // priority_queue qv // first: distance from st // second: this node using qv = pair<ll, int>; priority_queue<qv, vector<qv>, greater<qv>> q; dist[st] = 0; q.push(qv{dist[st], st}); while (!q.empty()) { qv now = q.top(); q.pop(); if (now.first > dist[now.second]) continue; for (edge ne : g[now.second]) { ll nd = dist[now.second] + ne.cost; if (dist[ne.to] > nd) { dist[ne.to] = nd; q.push(qv{dist[ne.to], ne.to}); } } } return; } }; int main() { int n; cin >> n; graph g(n); for (int i = 0; i < n - 1; i++) { int u, v, w; cin >> u >> v >> w; u--; v--; g.addEdgeBoth(u, v, w); } ll ans = 0; auto f = [&](auto self, int i, int prev = -1) -> ll { vector<ll> vs; for (auto [j, c] : g.g[i]) { if (j == prev) continue; vs.push_back(self(self, j, i) + c); } vs.push_back(0); sort(vs.rbegin(), vs.rend()); if (vs.size() == 1) { ans = max(ans, vs[0]); } else { ans = max(ans, vs[0] + vs[1]); } return vs[0]; }; f(f, 0); cout << ans << endl; return 0; }