- 先生成最小生成树,再尝试添入一条不在集合的边,删去一条在集合的边
- 对于添入一条边(u,v),则删去u,v在树上的最短路径上的最长边,在添入新边
- 此时各点依旧两两相通
- 找到u,v树上的最短路径上的最长边,利用树链剖分进行标号,再用线段树维护区间最大值
- 树链剖分将u,v的路径变为了多个标号连续的区间
- 注意边权向点权的转化
- 这里将1视为根节点
- 对于(u,v)边,在1为根节点时,(u,v)是u指向v的,所以将边权放到v上
- 1的点权为0
- 所有点点权初始为0
- 同时找区间最值得最后一个区间要去掉最高点的点权
- 因为该点权为上面一条边,不在最短路径上
import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;
//implements Runnable
public class Main{
static long md=(long)1e9+7;
static long Linf=Long.MAX_VALUE/2;
static int inf=Integer.MAX_VALUE/2;
static int N=100010;
static int n=0;
static int m=0;
static long ans=0;
static
class Edge{
int fr,to,nxt;
long val;
public Edge(int u,int v,long w){
fr=u;to=v;val=w;
}
}
static Edge[] e;
static int[] head;
static int cnt=0;
static void addEdge(int fr,int to,long val){
cnt++;
e[cnt]=new Edge(fr,to,val);
e[cnt].nxt=head[fr];
head[fr]=cnt;
}
static int[] fa;
static int find(int x){
if(x==fa[x])return x;
else return fa[x]=find(fa[x]);
}
static int[] siz,son,dep,top,id;
static long[] v,tr;
static void dfs1(int x,int f){
dep[x]=dep[f]+1;fa[x]=f;siz[x]=1;
for(int i=head[x];i>0;i=e[i].nxt){
int v=e[i].to;
if(v==f)continue;
dfs1(v,x);
siz[x]+=siz[v];
if(son[x]==0||siz[son[x]]<siz[v]){
son[x]=v;
}
}
}
static int o=0;
static void dfs2(int x,int tp,long w){
o++;
id[x]=o;
v[o]=w;
top[x]=tp;
for(int i=head[x];i>0;i=e[i].nxt){
int v=e[i].to;
if(v==fa[x])continue;
if(v==son[x]){
dfs2(v,tp,e[i].val);
break;
}
}//注意顺序,先重儿子
for(int i=head[x];i>0;i=e[i].nxt){
int v=e[i].to;
if(v==fa[x]||v==son[x])continue;
dfs2(v,v,e[i].val);
}
}
static void pushup(int x){
tr[x]=Math.max(tr[x<<1],tr[x<<1|1]);
}
static void build(int x,int l,int r){
if(l==r){
tr[x]=v[l];
return;
}
int mid=(l+r)>>1;
build(x<<1,l,mid);
build(x<<1|1,mid+1,r);
pushup(x);
}
static long query(int x,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr){
return tr[x];
}
int mid=(l+r)>>1;
long res=0;
if(ql<=mid)res=Math.max(res,query(x<<1,l,mid,ql,qr));
if(qr>mid) res=Math.max(res,query(x<<1|1,mid+1,r,ql,qr));
return res;
}
static long query_road(int x,int y){
long res=0;
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]){
int t=x;x=y;y=t;
}
res=Math.max(res,query(1,1,n,id[top[x]],id[x]));
x=fa[top[x]];
}
if(dep[x]<dep[y]){
int t=x;x=y;y=t;
}
if(id[x]!=id[y])res=Math.max(res,query(1,1,n,id[y]+1,id[x]));
return res;
}
static
class Node{
int x,y;long z;
public Node(int u,int v,long w){
x=u;y=v;z=w;
}
}
static Node[] p;
static void init(){
e=new Edge[2*n+1];
head=new int[2*n+1];
cnt=0;
fa=new int[2*n+1];son=new int[2*n+1];siz=new int[2*n+1];dep=new int[2*n+1];id=new int[2*n+1];
tr=new long[n<<2+1];v=new long[2*n+1];top=new int[2*n+1];
p=new Node[m+1];
}
static void solve() throws Exception{
//快读快输
AReader input=new AReader();
// Scanner input=new Scanner(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
String al="abcdefghijklmnopqrstuvwxyz";
char[] ac=al.toCharArray();
n=input.nextInt();
m=input.nextInt();
init();
for(int i=1;i<=m;++i){
p[i]=new Node(input.nextInt(),input.nextInt(), input.nextLong());
}
Arrays.sort(p,1,m+1,(o1,o2)->{
if(o1.z-o2.z>0)return 1;
else if(o1.z-o2.z<0)return -1;
else return 0;
});
for(int i=1;i<=n;++i)fa[i]=i;
int[] b=new int[m+1];int bb=0;
long A=0;
for(int i=1;i<=m;++i){
int x=p[i].x;int y=p[i].y;
x=find(x);y=find(y);
if(x==y){
bb++;
b[bb]=i;
continue;
}
fa[x]=y;A+=p[i].z;
addEdge(p[i].x,p[i].y,p[i].z);
addEdge(p[i].y,p[i].x,p[i].z);
}
Arrays.fill(fa,0);
dfs1(1,0);
dfs2(1,1,0);
build(1,1,n);
long ans=Linf;
for(int t=1;t<=bb;++t){
int i=b[t];
int x=p[i].x;int y=p[i].y;
long de=query_road(x,y);
ans=Math.min(ans,A-de+p[i].z);
}
out.write(ans+"\n");
out.flush();
out.close();
}
public static void main(String[] args) throws Exception{
solve();
}
// public static final void main(String[] args) throws Exception {
// new Thread(null, new Main(), "线程名字", 1 << 27).start();
// }
// @Override
// public void run() {
// try {
// //原本main函数的内容
// solve();
//
// } catch (Exception e) {
// }
// }
static
class AReader{
BufferedReader bf;
StringTokenizer st;
public AReader(){
bf=new BufferedReader(new InputStreamReader(System.in));
st=new StringTokenizer("");
}
public String next() throws IOException{
while(!st.hasMoreTokens()){
st=new StringTokenizer(bf.readLine());
}
return st.nextToken();
}
public int nextInt() throws IOException{
return Integer.parseInt(next());
}
public long nextLong() throws IOException{
return Long.parseLong(next());
}
public double nextDouble() throws IOException{
return Double.parseDouble(next());
}
}
}