标签:
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 200000/200000 K (Java/Others)
Total Submission(s): 2938 Accepted Submission(s): 902
1 # include <bits/stdc++.h> 2 using namespace std; 3 const int MAX = 100010; 4 typedef long long int ll; 5 struct Node 6 { 7 int to; 8 int next; 9 ll val; 10 }tree[MAX * 2]; 11 int head[MAX]; 12 int tol = 0; 13 int num[MAX * 2]; 14 ll sum = 0; 15 int n; 16 void add(int a, int b, ll val) 17 { 18 tree[tol].to = b; 19 tree[tol].next = head[a]; 20 tree[tol].val = val; 21 head[a] = tol++; 22 } 23 void dfs(int root, int f) 24 { 25 num[root] = 1; 26 for(int i = head[root]; i != -1; i = tree[i].next) 27 { 28 int son = tree[i].to; 29 if(son == f) 30 continue; 31 dfs(son, root); 32 33 num[root] += num[son]; 34 ll tep = min(num[son], n - num[son]); 35 sum += tep * (tree[i].val) * 2LL; 36 } 37 } 38 int main() 39 { 40 int t; 41 scanf("%d", &t); 42 int case_ = 0; 43 while(t--) 44 { 45 memset(head, -1, sizeof(head)); 46 memset(num, 0, sizeof(num)); 47 tol = 0; 48 sum = 0; 49 scanf("%d", &n); 50 int a, b; 51 ll val; 52 for(int i = 1; i < n; i++) 53 { 54 scanf("%d%d%I64d", &a, &b, &val); 55 add(a, b, val); 56 add(b, a, val); 57 } 58 dfs(1, -1); 59 printf("Case #%d: %I64d\n", ++case_, sum); 60 } 61 return 0; 62 }
HDU 4118 - Holiday's Accommodation
标签:
原文地址:http://www.cnblogs.com/lyf-acm/p/5816056.html