• $P3931 SAC E一道难题 Tree$


    problem

    #include <bits/stdc++.h>
    #define rep(i,j,n) for(register int i=j;i<=n;i++)
    #define Rep(i,j,n) for(register int i=j;i>=n;i--)
    #define low(x) x&(-x)
    using namespace std ;
    typedef long long LL ;
    const int inf = INT_MAX >> 1 ;
    inline LL In() { LL res(0) , f(1) ; register char c ;
    #define gc c = getchar()
    	while(isspace(gc)) ; c == '-' ? f = - 1 , gc : 0 ;
    	while(res = (res << 1) + (res << 3) + (c & 15) , isdigit(gc)) ;
    	return res * f ;
    #undef gc
    }
    
    int n , s ;
    const int N = 100000 + 5 ;
    const int M = N << 1 ;
    int nxt[M] ;
    int to[M] ;
    int head[N] ;
    LL dis[M] ;
    int tot(1) ;
    
    inline void Add(int u, int v, LL w) {
    	nxt[++ tot] = head[u] , head[u] = tot , dis[tot] = w , to[tot] = v ;
    }
    
    inline LL Dfs(int x,int y) {
    	LL ans = 0 ;
    	bool ck = 0 ;
    	for(register int i = head[x] ; i ; i = nxt[i]) {
    		if(to[i] == y) continue ;
    		ans += min(Dfs(to[i] , x) , dis[i]) , ck = 1 ;
    	}
    	if(ck == 0) return LLONG_MAX ;
    	return ans ;
    }
    
    inline void Ot() {
    	n = In() - 1 , s = In() ;
    	rep(i,1,n) {
    		int u = In() , v = In() ;
    		LL  w = In() ;
    		Add(u,v,w) , Add(v,u,w) ;
    	}
    	cout << Dfs(s , s) << endl ;
    }
    
    signed main() {
    	return Ot() , 0 ;
    }
    
    
  • 相关阅读:
    IDEA插件和快捷设置
    漫谈虚拟内存
    漫谈进程和线程
    漫谈计算机语言
    初识Python
    数据库物理设计
    漫谈计算机体系
    数据库逻辑设计
    NLP中几种分词库的简单使用(Python)
    ML————朴素贝叶斯原理和SKlearn相关库
  • 原文地址:https://www.cnblogs.com/qf-breeze/p/10657296.html
Copyright © 2020-2023  润新知