結果
| 問題 |
No.3113 The farthest point
|
| コンテスト | |
| ユーザー |
toku4388
|
| 提出日時 | 2025-04-20 01:53:40 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,591 bytes |
| コンパイル時間 | 4,014 ms |
| コンパイル使用メモリ | 292,148 KB |
| 実行使用メモリ | 19,556 KB |
| 最終ジャッジ日時 | 2025-04-20 01:53:51 |
| 合計ジャッジ時間 | 10,749 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 28 WA * 5 |
ソースコード
#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);
}
sort(vs.rbegin(), vs.rend());
if (vs.size() == 0) {
return 0;
} else 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;
}
toku4388