http://acm.hdu.edu.cn/showproblem.php?pid=3848
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#define N 10001
using namespace std;
struct node{
int u,step;
bool friend operator<(const node&x,const node&y){
return x.step>y.step;
}
};
struct edge{
int next,v,w;
edge(){};
edge(int a,int b,int c){
next=a;v=b;w=c;
}
}E[N*2];
int head[N],NE;
int in[N],n,Min;
void init(){
memset(in,0,sizeof(in));
NE=0;
memset(head,-1,sizeof(head));
}
void insert(int u,int v,int w){
E[NE]=edge(head[u],v,w);
head[u]=NE++;
}
int bfs(int s){
priority_queue<node> q;
node p,temp;
p.u=s;p.step=0;
q.push(p);
bool h[N];
memset(h,0,sizeof(h));
h[s]=1;
while(!q.empty()){
temp=q.top();
q.pop();
int u=temp.u;
if(in[u]==1&&u!=s)
return temp.step;
for(int i=head[u];i!=-1;i=E[i].next){
p=temp;
if(h[E[i].v]||p.step+E[i].w>Min)
continue;
p.u=E[i].v;
p.step+=E[i].w;
h[p.u]=1;
q.push(p);
}
}
return Min;
}
int main(void){
while(scanf("%d",&n),n){
init();
int u,v,w;
for(int i=1;i<n;i++){
scanf("%d%d%d",&u,&v,&w);
insert(u,v,w);
insert(v,u,w);
in[u]++;in[v]++;
}
if(n==2){
printf("%d\n",w);
continue;
}
Min=0x7fffffff;
for(int i=1;i<=n;i++)
if(in[i]==1){
int k=bfs(i);
if(k<Min)
Min=k;
}
printf("%d\n",Min);
}
}