#include using namespace std; template void read(T &t) { t=0; char ch=getchar(); int f=1; while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); } do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f; } const int INF=(1e9); int n,m,ans,tot,head[3010],nxt[200010],to[200010]; int fa[3010],link[3010],w[200010]; void add(int x,int y,int z) { tot++; nxt[tot]=head[x]; head[x]=tot; to[tot]=y; w[tot]=z; } int find(int x) { if (fa[x]==x) return x; return fa[x]=find(fa[x]); } void merge(int x,int y) { int z=x; while (link[z]) z=link[z]; link[z]=y; fa[y]=x; } bool vis[3010]; int val[3010]; priority_queue > q; int solve(int cnt,int &s,int &t) { t=1; for (int i=1;i<=n;i++) vis[i]=0,val[i]=0; q=priority_queue >(); pair A; while (cnt--) { s=t; vis[s]=1; for (int u=s;u;u=link[u]) for (int i=head[u],v;i;i=nxt[i]) { v=find(to[i]); if (!vis[v]) { val[v]+=w[i]; q.push(make_pair(val[v],v)); } } t=0; while (!t) { if (q.empty()) return 0; A=q.top(); q.pop(); if (val[A.second]==A.first) t=A.second; } } return val[t]; } int main() { //freopen("1.txt","r",stdin); int x,y,z; while (scanf("%d %d",&n,&m)!=EOF) { for (int i=1;i<=n;i++) head[i]=0,fa[i]=i,link[i]=0; tot=0; while (m--) { read(x); read(y); read(z); add(x,y,z); add(y,x,z); } ans=INF; for (int i=n-1,s,t;i>=1;i--) { ans=min(ans,solve(i,s,t)); if (!ans) break; merge(s,t); } printf("%d\n",ans); } return 0; } /* 0. Enough array size? Enough array size? Enough array size? Interger overflow? 1. Think TWICE, Code ONCE! Are there any counterexamples to your algo? 2. Be careful about the BOUNDARIES! N=1? P=1? Something about 0? 3. Do not make STUPID MISTAKES! Time complexity? Memory usage? Precision error? */