感受
真水题,满足感爆棚


图片说明
直接上代码了,其实树形DP就是在树上进行DP


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
const ll inf = 1e18;
struct edge{
    int v, nex; ll w;
}e[maxn << 1];
int head[maxn];
int n, m, s, cnt;
int deg[maxn];
ll dp[maxn];///dp[i]表示从i点出发不能到达其任意叶子节点的最小代价
void add_edge(int u, int v, ll w){
    cnt++;
    e[cnt] = (edge){v, head[u], w};
    head[u] = cnt;
}
void dfs(int u, int f){
    if(deg[u] == 1 && u != s){
        dp[u] = inf;
        return ;
    }
    ll ans = 0;
    for(int i = head[u]; i > 0; i = e[i].nex){
        if(e[i].v == f) continue;
        dfs(e[i].v, u);
        ans += min(e[i].w, dp[e[i].v]);
        //printf("ans = %lld\n", dp[e[i].v]);
    }
    dp[u] = ans;
    return ;
}
int main(){
    scanf("%d%d%d", &n, &m, &s);
    int u, v; ll w;
    for(int i = 1; i < n; i++){
        scanf("%d%d%lld", &u, &v, &w);
        add_edge(u, v, w); add_edge(v, u, w);
        deg[u]++; deg[v]++;
    }
    dfs(s, 0);
    printf("%lld\n", dp[s]);
    return 0;
}