fr.cpp
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <climits>
using namespace std;
int findMin(int parrent,int current, int n,int edges[1005][1005]){
int minEffort = 0;
bool hasOffspring = false;
for(int i = 0; i < n; i++){
if(i == parrent) continue;
if(edges[current][i] > 0){
minEffort += findMin(current,i,n,edges);
hasOffspring = true;
}
}
if(!hasOffspring) return edges[parrent][current];
return min(minEffort,edges[parrent][current]);
}
int main(){
int n = 0;
int c = 0;
int edges[1005][1005];
while(cin >> n >> c){
for(int i = 0;i<=n+1;i++){
for(int j=0;j<=n+1;j++){
edges[i][j] = -1;
}
}
int u,v,w;
for(int i=0;i<n - 1;i++){
cin >> u >> v >> w;
edges[u][v] = w;
edges[v][u] = w;
}
int minEffort = 0;
for(int i = 1; i < n+1; i++){
if(edges[c][i] > 0){
minEffort += findMin(c,i,n,edges);
}
}
cout << minEffort << endl;
}
return 0;
}