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
| #include<bits/stdc++.h> #define RG register #define file(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout); #define for_edge(i, x) for(RG int i=head[x];i;i=e[i].next) #define clear(x, y) memset(x, y, sizeof(x)); using namespace std;
template<typename T> inline T read() { T data=0, w=1; char ch=getchar(); while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar(); if(ch=='-') w=-1, ch=getchar(); while(ch>='0'&&ch<='9') data=(data<<3)+(data<<1)+(ch^48), ch=getchar(); return data*w; }
const int maxn(100010), maxm(300010); struct edge { int next, to; long long dis; } e[maxn << 1]; int head[maxn], e_num; inline void add_edge(int from, int to, long long dis) { e[++e_num]={head[from], to, dis}; head[from]=e_num; }
struct edge_k { int from, to; long long dis; } edg[maxm]; inline bool cmp(const edge_k &a, const edge_k &b) { return a.dis<b.dis; } int fa[maxn], n, m; long long value; bool use[maxm]; inline int find(const int &x) { return fa[x] == x ? x : fa[x]=find(fa[x]); } inline void mst() { long long ans=0; for(RG int i=1;i<=n;i++) fa[i]=i; for(RG int i=1;i<=m;i++) { int x=find(edg[i].from), y=find(edg[i].to); if(x!=y) { fa[max(x, y)]=min(x, y); ans+=edg[i].dis; use[i]=true; add_edge(edg[i].from, edg[i].to, edg[i].dis); add_edge(edg[i].to, edg[i].from, edg[i].dis); } } value = ans; }
long long fst[maxn][18], sec[maxn][18], ANS=0; int f[maxn][18], deep[maxn]; inline void dfs(int x) { for_edge(i, x) { int to=e[i].to; long long ds=e[i].dis; if(to==f[x][0]) continue; f[to][0]=x; fst[to][0]=sec[to][0]=ds; deep[to]=deep[x]+1; dfs(to); } } inline void get_sec(long long &sec, long long a, long long b, long long c, long long d, long long _max) { if(a<_max) sec=a; if(b<_max) sec=max(sec, b); if(c<_max) sec=max(sec, c); if(d<_max) sec=max(sec, d); } inline void init() { dfs(1); for(RG int j=1;j<18;j++) for(RG int i=1;i<=n;i++) { f[i][j]=f[f[i][j-1]][j-1]; long long f1=fst[i][j-1], f2=fst[f[i][j-1]][j-1], s1=sec[i][j-1], s2=sec[f[i][j-1]][j-1]; fst[i][j]=max(f1, f2); get_sec(sec[i][j], f1, f2, s1, s2, fst[i][j]); } }
inline long long query(int a, int b, long long dis) { if(deep[a]<deep[b]) swap(a, b); long long fsa=-1, sca=-1, fsb=-1, scb=-1; int d=deep[a]-deep[b]; for(RG int i=0;(1<<i)<=d;i++) { if((1<<i)&d) { long long tmp=sca; get_sec(sca, fsa, fst[a][i], tmp, sec[a][i], max(fsa, fst[a][i])); fsa=max(fsa, fst[a][i]); a=f[a][i]; } } if(a==b) { long long tmp=sca; get_sec(sca, fsa, fsb, tmp, scb, max(fsa, fsb)); if(dis==max(fsa, fsb)) return value-sca+dis; else return value-max(fsa, fsb)+dis; } for(RG int i=17;~i;i--) { if(f[a][i]!=f[b][i]) { long long tmp=sca; get_sec(sca, fsa, fst[a][i], tmp, sec[a][i], max(fsa, fst[a][i])); fsa=max(fsa, fst[a][i]); a=f[a][i]; tmp=scb; get_sec(scb, fsb, fst[b][i], tmp, sec[b][i], max(fsb, fst[b][i])); fsb=max(fsb, fst[b][i]); b=f[b][i]; } } long long tmp=sca; get_sec(sca, fsa, fst[a][0], tmp, sec[a][0], max(fsa, fst[a][0])); fsa=max(fsa, fst[a][0]); a=f[a][0]; tmp=scb; get_sec(scb, fsb, fst[b][0], tmp, sec[b][0], max(fsb, fst[b][0])); fsb=max(fsb, fst[b][0]); b=f[b][0]; tmp=sca; get_sec(sca, fsa, fsb, tmp, scb, max(fsa, fsb)); if(dis==max(fsa, fsb)) return value-sca+dis; else return value-max(fsa, fsb)+dis; }
const long long I(9223372036854775807ll); int main() { n=read<int>(); m=read<int>(); for(RG int i=1;i<=m;i++) edg[i].from=read<int>(), edg[i].to=read<int>(), edg[i].dis=read<long long>(); sort(edg+1, edg+m+1, cmp); mst(); init(); ANS=I; for(RG int i=1;i<=m;i++) if(!use[i]) ANS=min(ANS, query(edg[i].from, edg[i].to, edg[i].dis)); printf("%lld\n", ANS); return 0; }
|