1. 程式人生 > >2435: [Noi2011]道路修建

2435: [Noi2011]道路修建

bsp geo getchar() tar -- enum std isdigit main

2435: [Noi2011]道路修建

https://www.lydsy.com/JudgeOnline/problem.php?id=2435

 1 /*
 2 
 3 開始讀錯題了,以為順序修建給定的邊,計算費用,算的樣例是12。
 4 每條邊的權值就是兩邊的點的差的絕對值*這條邊的長度。
 5 dfs一遍 
 6 
 7 */
 8 
 9 #include<bits/stdc++.h>
10 using namespace std;
11 typedef long long LL;
12  
13 inline int read() {
14 int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch==-)f=-1; 15 for (;isdigit(ch);ch=getchar())x=x*10+ch-0;return x*f; 16 } 17 18 const int N = 1000005; 19 int head[N],nxt[N<<1],to[N<<1],val[N<<1],siz[N],Enum,Sum; 20 LL Ans; // --- 21 22 inline void add_edge(int
u,int v,int w) { 23 ++Enum;to[Enum] = v, val[Enum] = w, nxt[Enum] = head[u],head[u] = Enum; 24 } 25 void dfs(int u,int fa) { 26 siz[u] = 1; 27 for (int i=head[u]; i; i=nxt[i]) { 28 int v = to[i]; 29 if (v == fa) continue; 30 dfs(v,u); 31 siz[u] += siz[v];
32 Ans += 1ll * abs(Sum - siz[v] - siz[v]) * val[i]; 33 } 34 } 35 int main() { 36 int n = read();Sum = n; 37 for (int i=1; i<n; ++i) { 38 int u = read(),v = read(),w = read(); 39 add_edge(u,v,w);add_edge(v,u,w); 40 } 41 dfs(1,0); 42 cout << Ans; 43 return 0; 44 }

2435: [Noi2011]道路修建