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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <string> #include <vector>
#define DEBUG(x) std::cerr << #x << " = " << x << std::endl;
using std::cin; using std::cout; using std::endl;
inline int read() { int s = 0, x = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') x = -x; ch = getchar(); } while (isdigit(ch)) { s = s * 10 + ch - '0'; ch = getchar(); } return s * x; }
const int MAXN = 100000 + 10; const int MAXM = 300000 + 10; const int MAXLOG = 17 + 3; const int LOGM = 17;
struct Edge { int v, w; Edge(int _v = 0, int _w = 0) : v(_v), w(_w) {} };
struct REdge { int u, v, w; bool chose; REdge() { u = v = w = 0; chose = 0; } void _r() { u = read(); v = read(); w = read(); } bool operator < (const REdge &th) const { return w < th.w; } } edge[MAXM];
struct DSU { int u[MAXN]; DSU() { memset(u, 0, sizeof u); } int find(int x) { return !u[x] ? x : u[x] = find(u[x]); } bool merge(int x, int y) { x = find(x); y = find(y); if (x == y) return false; u[x] = y; return true; } } kk;
int n, m;
int twon[MAXLOG];
std::vector<Edge> G[MAXN]; int fa[MAXN][MAXLOG], depth[MAXN];
int maxw[MAXN][MAXLOG][2]; long long int minDelta = 0x3f3f3f3f3f3f3f3f;
long long int Kruskal() { long long int ans = 0; int ch = 0; std::sort(edge + 1, edge + 1 + m); for (int i = 1; i <= m; ++i) { if (kk.merge(edge[i].u, edge[i].v)) { ++ch; edge[i].chose = true; ans += 1ll * edge[i].w; G[edge[i].u].push_back(Edge(edge[i].v, edge[i].w)); G[edge[i].v].push_back(Edge(edge[i].u, edge[i].w)); } if (ch == n - 1) break; } return ans; }
void dfs(int u) { for (int i = 1; depth[u] >= twon[i]; ++i) { fa[u][i] = fa[fa[u][i - 1]][i - 1]; maxw[u][i][0] = std::max(maxw[u][i - 1][0], maxw[fa[u][i - 1]][i - 1][0]); if (maxw[u][i - 1][0] == maxw[fa[u][i - 1]][i - 1][0]) { maxw[u][i][1] = std::max(maxw[u][i - 1][1], maxw[fa[u][i - 1]][i - 1][1]); } else maxw[u][i][1] = std::max( std::min(maxw[u][i - 1][0], maxw[fa[u][i - 1]][i - 1][0]), std::max(maxw[u][i - 1][1], maxw[fa[u][i - 1]][i - 1][1]) ); } for (int i = 0, siz = (int) G[u].size(); i < siz; ++i) { int v = G[u][i].v, w = G[u][i].w; if (v == fa[u][0]) continue; maxw[v][0][0] = w; maxw[v][0][1] = -1; fa[v][0] = u; depth[v] = depth[u] + 1; dfs(v); } }
void initLCA() { twon[0] = 1; for (int i = 1; i <= LOGM; ++i) { twon[i] = twon[i - 1] * 2; } depth[1] = 1; dfs(1); }
int LCA(int x, int y) { if (depth[x] < depth[y]) std::swap(x, y); int k = depth[x] - depth[y]; for (int i = 0; i <= LOGM; ++i) { if (k & (1 << i)) x = fa[x][i]; } if (x == y) return x; for (int i = LOGM; i >= 0; --i) { if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i]; } return fa[x][0]; }
void GetLeastDelta(int x, int y, int dw) { int mx = 0, smx = 0, k = depth[x] - depth[y]; for (int i = 0; i <= LOGM; ++i) { if (k & (1 << i)) { if (smx < maxw[x][i][1]) { smx = maxw[x][i][1]; } if (mx < maxw[x][i][0]) { smx = std::max(smx, mx); mx = maxw[x][i][0]; } } } if (mx == dw) minDelta = std::min(minDelta, 1ll * (dw - smx)); else minDelta = std::min(minDelta, 1ll * (dw - mx)); }
int main() { n = read(); m = read(); for (int i = 1; i <= m; ++i) edge[i]._r(); long long int mst = Kruskal(); initLCA(); for (int i = 1; i <= m; ++i) { if (!edge[i].chose) { int l = LCA(edge[i].u, edge[i].v); GetLeastDelta(edge[i].u, l, edge[i].w); GetLeastDelta(edge[i].v, l, edge[i].w); } } printf("%lld\n", mst + minDelta); return 0; }
|