#include<stdio.h> #include<string.h> #include<stdlib.h> const int maxn=30010,maxm=150010,INF=100000000; struct node{ int x,y,w,f; }; struct node a[maxm],tmp; int f[maxn],rank[maxn],pre[maxn],pres[maxn],p[maxn],n,ansx[maxm]; int e,to[maxm],next[maxm],begin[maxn],ss[maxm]; void qsort(int l,int r){ int mid=a[(l+r)/2].w,i,j; i=l;j=r; while(i<=j){ while(i<=j && a[i].w<mid)i++; while(i<=j && a[j].w>mid)j--; if(i<=j){ tmp=a[i];a[i]=a[j];a[j]=tmp; i++;j--; } } if(l<j)qsort(l,j); if(i<r)qsort(i,r); } int fa(int u){ if(f[u]!=u) f[u]=fa(f[u]); return f[u]; } int find(int u,int v){ return fa(u)==fa(v)?0:1; } void link(int u,int v){ if(rank[fa(u)]>=rank[fa(v)]){ f[fa(v)]=fa(u); rank[fa(u)]++; }else{ f[fa(u)]=fa(v); rank[fa(v)]++; } } void dfs(int u,int v){ int i,j,k; if(u==v)return; p[u]=1; for(i=begin[u];i;i=next[i]){ k=to[i]; if(!p[k]){ pre[k]=u; pres[k]=ss[i]; dfs(k,v); } } } int main(){ #ifndef ONLINE_JUDGE freopen("prim2.in","r",stdin); freopen("kruscal.out","w",stdout); #endif int i,j,k,m,s; scanf("%d%d",&n,&m); for(i=1;i<=n;i++)f[i]=i; for(i=1;i<=m;i++) scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w); qsort(1,m); s=n-1;i=0; int ans=0; while(s && i<=m){ i++; if(find(a[i].x,a[i].y)){ link(a[i].x,a[i].y); to[++e]=a[i].y; next[e]=begin[a[i].x]; begin[a[i].x]=e; ss[e]=a[i].w; to[++e]=a[i].x; next[e]=begin[a[i].y]; begin[a[i].y]=e; ss[e]=a[i].w; a[i].f=1; s--; ans+=a[i].w; } } printf("%d\n",ans); int t=0; for(i=1;i<=m;i++){ if(a[i].f==0){ memset(p,0,sizeof(p)); dfs(a[i].x,a[i].y); j=a[i].y; int max=0; while(j!=a[i].x){ if(pres[j]>max) max=pres[j]; j=pre[j]; } ansx[++t]=ans-max+a[i].w; } } int minans=INF; for(i=1;i<=t;i++)if(ansx[i]!=ans && ansx[i]<minans)minans=ansx[i]; printf("%d\n",minans); return 0; }