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 163 164 165
| struct node{ int from, to, next; ll w; }edge[maxn<<1], e[N];
int head[maxn], cnt; inline void add(int u,int v, ll w){ edge[++cnt].w = w; edge[cnt].to = v; edge[cnt].next = head[u]; head[u] = cnt; } int cmp(node a, node b){ return a.w < b.w; }
int id[maxn]; inline void init(void){ for(int i = 0; i < maxn;i++) id[i] = i; cnt = 0; } inline int find(int p){ if(p == id[p]) return p; return id[p] = find(id[p]); } inline int funion(int p, int q){ int fp = find(p); int fq = find(q); if(fp == fq) return 0; id[fp] = fq; return 1; }
int n, m; int flag[N]; inline ll krusral(void){ init(); sort(e, e+m, cmp); ll ans = 0; for(int i = 0;i < m;i++){ if(funion(e[i].from, e[i].to)){ flag[i] = 1; add(e[i].from, e[i].to, e[i].w); add(e[i].to, e[i].from, e[i].w); ans += e[i].w; } } return ans; }
ll mx[maxn][20]; ll smx[maxn][20]; int fa[maxn][20]; int deep[maxn]; inline void dfs(int now, int f){ deep[now] = deep[f]+1; fa[now][0] = f; for(int i = 1;i <= (int)log2(deep[now]);i++){ fa[now][i] = fa[fa[now][i-1]][i-1]; mx[now][i] = max(mx[now][i-1], mx[fa[now][i-1]][i-1]); if(mx[now][i-1] == mx[fa[now][i-1]][i-1]) smx[now][i] = max(smx[now][i-1], smx[fa[now][i-1]][i-1]); else smx[now][i] = max(min(mx[now][i-1], mx[fa[now][i-1]][i-1]), max(smx[now][i-1], smx[fa[now][i-1]][i-1])); } for(int i = head[now];i;i = edge[i].next){ if(edge[i].to == f) continue; mx[edge[i].to][0] = edge[i].w; smx[edge[i].to][0] = -1; dfs(edge[i].to, now); } }
inline ll lca(int u, int v, ll w){ if(u == v) return (ll)1e9; ll ans1 = 0, ans2 = 0; if(deep[u] < deep[v]) swap(u, v); while(deep[u] > deep[v]){ int mid = (int)log2(deep[u] - deep[v]); if(mx[u][mid] == ans1){ ans2 = max(ans2, smx[u][mid]); }else{ ans2 = max(min(ans1, mx[u][mid]), max(ans2, smx[u][mid])); } ans1 = max(ans1, mx[u][mid]); u = fa[u][mid]; } if(u == v) return ((w - ans1) == 0) ? (w - ans2) : (w-ans1); for(int i = (int)log2(deep[u]);i >= 0;i--){ if(fa[u][i] != fa[v][i]){ if(mx[u][i] == ans1) ans2 = max(ans2, smx[u][i]); else{ ans2 = max(min(ans1, mx[u][i]), max(ans2, smx[u][i])); } ans1 = max(ans1, mx[u][i]);
if(mx[v][i] == ans1) ans2 = max(ans2, smx[v][i]); else{ ans2 = max(min(ans1, mx[v][i]), max(ans2, smx[v][i])); } ans1 = max(ans1, mx[v][i]); u = fa[u][i]; v = fa[v][i]; } } int i = 0; if(mx[u][i] == ans1) ans2 = max(ans2, smx[u][i]); else{ ans2 = max(min(ans1, mx[u][i]), max(ans2, smx[u][i])); } ans1 = max(ans1, mx[u][i]); if(mx[v][i] == ans1) ans2 = max(ans2, smx[v][i]); else{ ans2 = max(min(ans1, mx[v][i]), max(ans2, smx[v][i])); } ans1 = max(ans1, mx[v][i]); return ((w - ans1) == 0) ? (w - ans2) : (w-ans1); }
inline void solve(void){ read(n, m); for(int i = 0;i < m;i++){ read(e[i].from, e[i].to, e[i].w); } ll temp = krusral(); dfs(1, 0); ll ans = 1e9; for(int i = 0;i < m;i++){ if(!flag[i]){ ll mid = lca(e[i].from, e[i].to, e[i].w); ans = min(ans, mid); } } write('\n', temp + ans); }
signed main(void){
#ifdef _DEBUG freopen("in.in","r",stdin); freopen("out.out","w",stdout); #endif
int t = 1; while(t--){ solve(); }
return 0; }
|