$P3931 SAC E一道难题 Tree$
#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 ;
}
随机推荐
- python爬虫25 | 爬取下来的数据怎么保存? CSV 了解一下
大家好 我是小帅b 是一个练习时长两年半的练习生 喜欢 唱! 跳! rap! 篮球! 敲代码! 装逼! 不好意思 我又走错片场了 接下来的几篇文章 小帅b将告诉你 如何将你爬取到的数据保存下来 有文本 ...
- 基于XML文档的声明式事务配置
<!-- 配置事务切面 --> <aop:config> <aop:pointcut expression="execution(* com.atguigu.t ...
- RequestMapping注解_修饰类
[使用RequestMapping映射请求] 1.Spring MVC使用 @RequestMapping 注解为控制器指定可以处理哪些URL请求. 2.在控制器的类定义及方法定义处都可以标注. @R ...
- 【Codeforces 711C】Coloring Trees
[链接] 我是链接,点我呀:) [题意] 连续相同的数字分为一段 你可以改变其中0为1~m中的某个数字(改变成不同数字需要不同花费) 问你最后如果要求分成恰好k段的话,最少需要多少花费 [题解] dp ...
- 【Codeforces 486C】Palindrome Transformation
[链接] 我是链接,点我呀:) [题意] 光标一开始在p的位置 你可以用上下左右四个键位移动光标(左右)或者更改光标所在的字符(上下增加或减少ascill码) 问你最少要操作多少次才能使得字符串变成回 ...
- [bzoj1022/poj3480]小约翰的游戏John_博弈论
小约翰的游戏John 题目大意:Nim游戏.区别在于取走最后一颗石子这输. 注释:$1\le cases \le 500$,$1\le n\le 50$. 想法:anti-SG游戏Colon定理. 如 ...
- 洛谷—— P3811 【模板】乘法逆元
https://www.luogu.org/problem/show?pid=3811 题目背景 这是一道模板题 题目描述 给定n,p求1~n中所有整数在模p意义下的乘法逆元. 输入输出格式 输入格式 ...
- endWith is not a function
解决方法,增加String的扩展 String.prototype.endWith = function(suffix) { return this.indexOf(suffix, this.leng ...
- Ubuntu 16.04添加启动图标到Dash Home中
一.添加图标: 图标信息在以下两处地方: /usr/share/applications ~/.local/share/applications(用户独立配置存放地方,是个隐藏文件夹) 图标信息文件以 ...
- 1.7-BGP③
IBGP的水平分隔原则(Split Horizon Rule): IBGP的水平分割原则:by default,routes learned via IBGP are never propagated ...