problem
solution
codes
#include<iostream>
#include<vector>
using namespace std;
typedef long long LL;
const int N = (int)1e5+10, inf = 1e9;
int n, S;
struct node{
LL to, v;
node(LL to, LL v):to(to),v(v){};
};
vector<node>G[N];
LL f[N];
void dp(int u, int from){
if(G[u].size()==1 && u!=S)f[u] = inf;
for(int i = 0; i < G[u].size(); i++){
LL v = G[u][i].to, t = G[u][i].v;
if(v == from)continue;
dp(v,u);
f[u] += min(t, f[v]);
}
}
int main(){
cin>>n>>S;
for(int i = 1; i <= n-1; i++){
int a, b, c; cin>>a>>b>>c;
G[a].push_back(node(b,c));
G[b].push_back(node(a,c));
}
dp(S,-1);
cout<<f[S]<<"\n";
return 0;
}