竞赛讨论区 > 只过了80谁能告诉我哪里漏了
头像
waxyt
发布于 2021-11-08 20:34
+ 关注

只过了80谁能告诉我哪里漏了

#include<bits/stdc++.h>
using namespace std;
const int N=50010;
int h[N],ne[N],e[N],w[N],idx;
bool st[N];
int n,x;
void add(int a,int b,int we){
    e[idx]=b;
    ne[idx]=h[a];
    w[idx]=we;
    h[a]=idx++;
}
int res=0,ans=0;
void dfs(int u){
   ans=max(res,ans);
    st[u]=true;
    for(int i=h[u];i!=-1;i=ne[i]){
        int j =e[i];
        if(!st[j]){
           st[j]=true;
            res+=w[i];
            dfs(j);
           st[j]=false;
            res-=w[i];
        }
    }
    
}
int main(){
    memset(h,-1,sizeof(h));
    cin>>n>>x;
    int sum=0;
    for(int i=2;i<=n;i++){
      int u,v,w;
        cin>>u>>v>>w;
        add(u,v,w);
        add(v,u,w);
        sum+=w;
    }
    dfs(x); 
    cout<<sum*2-ans;
    return 0;
}

全部评论

(0) 回帖
加载中...
话题 回帖

本文相关内容

等你来战

查看全部

热门推荐