zoukankan      html  css  js  c++  java
  • $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 ;
    }
    
    
  • 相关阅读:
    第4章 函数
    第3章 文件处理和函数
    第2章 数据类型
    第 8章 面向对象补充和网络编程
    第7章 面向对象
    第6章 常用模块
    第 5 章 函数的补充和模块加载
    git 使用命令提交文件
    jquery 读取本地json文件数据
    mpvue 封装api请求接口
  • 原文地址:https://www.cnblogs.com/qf-breeze/p/10657296.html
Copyright © 2011-2022 走看看