$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 ;
}

posted @ 2019-04-04 22:08  Isaunoya  阅读(127)  评论(0编辑  收藏  举报
TOP