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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
| #include <bits/stdc++.h> #define int long long
using namespace std; const int N = 1e5 + 5, M = 3e5 + 5, inf = 0x3f3f3f3f; int fa[N], dist[N*2], n, m; int h[N], e[M], ne[M], w[M], idx; int depth[N], f[N][17], d1[N][17], d2[N][17];
struct Node { int a, b, w; bool used; bool operator<(const Node &t) const { return w < t.w; } }E[M];
void add (int a, int b, int c) { e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++; }
int find (int x) { if (x != fa[x]) fa[x] = find (fa[x]); return fa[x]; }
int kruscal () { for (int i = 1; i <= n; i++) fa[i] = i; sort (E, E + m); int ans = 0; for (int i = 0; i < m; i++) { int a = E[i].a, b = E[i].b, w = E[i].w; int pa = find (a), pb = find (b); if (pa != pb) { fa[pa] = pb; ans += w; E[i].used = true; } } return ans; }
void build () { memset (h, -1, sizeof h); for (int i = 0; i < m; i++) { if (!E[i].used) continue; int a = E[i].a, b = E[i].b, w = E[i].w; add (a, b, w), add (b, a, w); } }
void bfs () { memset (depth, 0x3f, sizeof depth); depth[0] = 0, depth[1] = 1; queue <int> q; q.push (1);
while (!q.empty()) { int t = q.front(); q.pop();
for (int i = h[t]; ~i; i = ne[i]) { int j = e[i]; if (depth[j] > depth[t] + 1) { depth[j] =depth[t] + 1; q.push (j); f[j][0] = t; d1[j][0] = w[i], d2[j][0] = -inf; for (int k = 1; k <= 16; k++) { int anc = f[j][k-1]; f[j][k] = f[anc][k-1]; int dis[] = {d1[j][k-1], d2[j][k-1], d1[anc][k-1], d2[anc][k-1]}; d1[j][k] = d2[j][k] = -inf; for (int u = 0; u < 4; u++) { int d = dis[u]; if (d > d1[j][k]) d2[j][k] = d1[j][k], d1[j][k] = d; else if (d != d1[j][k] && d > d2[j][k]) d2[j][k] = d; } } } } } }
int lca (int a, int b, int w) { int cnt = 0; if (depth[a] < depth[b]) swap (a, b); for (int k = 16; k >= 0; k--) { if (depth[f[a][k]] >= depth[b]) { dist[cnt++] = d1[a][k]; dist[cnt++] = d2[a][k]; a = f[a][k]; } }
if (a != b) { for (int k = 16; k >= 0; k--) { if (f[a][k] == f[b][k]) continue; dist[cnt++] = d1[a][k]; dist[cnt++] = d2[a][k]; dist[cnt++] = d1[b][k]; dist[cnt++] = d2[b][k]; a = f[a][k], b = f[b][k]; } dist[cnt++] = d1[a][0]; dist[cnt++] = d1[b][0]; }
int dist1 = -inf, dist2 = -inf; for (int i = 0; i < cnt; i++) { int d = dist[i]; if (d > dist1) dist2 = dist1, dist1 = d; else if (d != dist1 && d > dist2) dist2 = d; }
if (w > dist1) return w - dist1; if (w > dist2) return w - dist2; return inf; }
signed main () { ios::sync_with_stdio (0);cin.tie(0); cin >> n >> m; for (int i = 0; i < m; i++) { int a, b, c; cin >> a >> b >> c; E[i] = {a, b, c}; }
int sum = kruscal(); build(), bfs();
int ans = 1e18; for (int i = 0; i < m; i++) { if (E[i].used) continue; int a = E[i].a, b = E[i].b, w = E[i].w; ans = min (ans, sum + lca (a, b, w)); }
cout << ans; }
|