1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| void solve() { int n, m;cin >> n >> m; vector<vector<pair<int, int>>>g(n + 1); vector<array<int, 3>>edge; for (int i = 1;i <= m;i++) { int u, v, w;cin >> u >> v >> w; g[u].push_back({ v,w }); g[v].push_back({ u,w }); edge.push_back({ u,v,w }); } vector<int>dis1(n + 10, 1e18), dis2(n + 10, 1e18); vector<int>st1(n + 10, 0), st2(n + 10, 0); auto dig1 = [&](int fi, int se) { priority_queue<PII, vector<PII>, greater<>>heap; heap.push({ 0, fi }); dis1[fi] = 0; while (heap.size()) { auto [d, u] = heap.top();heap.pop(); if (st1[u]) continue; st1[u] = 1; for (auto [i, w] : g[u]) { if (max(w, dis1[u]) < dis1[i]) { dis1[i] = max(w, dis1[u]); heap.push({ dis1[i],i }); } } } }; auto dig2 = [&](int fi, int se) { priority_queue<PII, vector<PII>, greater<>>heap; heap.push({ 0, fi }); dis2[fi] = 0; while (heap.size()) { auto [d, u] = heap.top();heap.pop(); if (st2[u]) continue; st2[u] = 1; for (auto [i, w] : g[u]) { if (max(w, dis2[u]) < dis2[i]) { dis2[i] = max(w, dis2[u]); heap.push({ dis2[i],i }); } } } }; dig1(1, n); dig2(n, 1); int ans = 1e18; for (auto i : edge) { auto [u, v, w] = i; if (w >= dis1[u] && w >= dis2[v]) { ans = min(ans, max(dis1[u], dis2[v]) + w); } if (w >= dis2[u] && w >= dis1[v]) { ans = min(ans, max(dis2[u], dis1[v]) + w); } } cout << ans << "\n"; }
|