now she wants to know the shortest time to get two apples;
The first line of every case there is a number N(2<=N<=10000)
if n is 0 means the end of input.
Next there is n-1 lines,on the i+1 line there is three number ai,bi,ci
which means there is a branch connect node ai and node bi.
(1<=ai, bi<=N , 1<=ci<=2000)
ci means the len of the branch is ci meters ;
7 1 2 1 2 3 2 3 4 1 4 5 1 3 6 3 6 7 4 0
5
//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int inf=(1<<28);
const int maxn=21000;
struct Node
{
int t,w;
int next;
};
int n;
int p[maxn];
int du[maxn];
int l;
Node G[maxn];
int ans;
int min1[maxn],min2[maxn];
void init()
{
memset(du,0,sizeof(du));
memset(p,-1,sizeof(p));
l=0;
ans=inf;
}
void addedge(int u,int t,int w)
{
G[l].t=t;
G[l].w=w;
G[l].next=p[u];
p[u]=l++;
}
int temp[4];
int pl;
void dfs(int u,int fath)
{
if(du[u]==1&&u!=1)
{
min1[u]=0;min2[u]=inf;
return ;
}
min1[u]=min2[u]=inf;
for(int i=p[u];i!=-1;i=G[i].next)
{
int t=G[i].t,w=G[i].w;
if(t==fath) continue;
dfs(t,u);
temp[0]=min1[u],temp[1]=min2[u];
temp[2]=min1[t]+w,temp[3]=min2[t]+w;
sort(temp,temp+4);
min1[u]=temp[0],min2[u]=temp[1];
}
ans=min(ans,min1[u]+min2[u]);
}
int main()
{
while(scanf("%d",&n)==1&&n)
{
init();
for(int i=0;i<n-1;i++)
{
int u,t,w;scanf("%d%d%d",&u,&t,&w);
addedge(u,t,w);
addedge(t,u,w);
du[u]++,du[t]++;
}
dfs(1,-1);
if(du[1]==1) ans=min(ans,min1[1]);
printf("%d\n",ans);
}
return 0;
}