原题解链接:https://ac.nowcoder.com/discuss/149984

本场饲喂量小的题目

发现从xx出发,只有一条路径可以只经过一次,其他的都会经过两次

找出这条最长的路径,用所有路径权值之和*2,减去这条路径的就好了

#include<bits/stdc++.h> 
using namespace std; 
inline int read() { 
    int x = 0,f= 1; 
    char c = getchar(); 
    while(c < '0' || c > '9')c = getchar(); 
    while(c <= '9' &&c >= '0')x = x * 10 + c - '0',c = getchar(); 
    return x * f; 
} 
int n,x; 
struct node {
    int v,w,next; 
} edge[100007]; 
int num = 0,head[100007]; 
inline void add_edge(int u,int v,int w) { 
    edge[++ num].v = v;edge[num].w = w; edge[num].next = head[u]; head[u] = num; 
} 
long long dfs(int x,int fa) {
    long long ret = 0; 
    for(int i = head[x];i ;i = edge[i].next) { 
        int v = edge[i].v; 
        if(v == fa) continue; 
        ret = std::max(ret,dfs(v,x) + edge[i].w); 
    } 
    return ret; 
} 
int main() {  
        num = 0; 
        memset(head,0,sizeof head); 
        scanf("%d%d",&n,&x); 
        long long ans =0 ; 
        for(int u,v,w,i = 1;i < n;++ i) { 
            u = read(),v = read(),w = read(); 
            add_edge(u,v,w); 
            add_edge(v,u,w); 
            ans += w; 
        } 	 
        ans *= 2; 
        ans -= dfs(x,0); 
        printf("%lld\n",ans); 
} 
/* 
8 4
3 1 717
3 6 93
6 5 367
7 4 154
4 2 665
8 3 402
3 7 795
*/