UVaLive6039 Uva1668 Let's Go Green
一开始考虑所有边都是单独的一条路径
然后尽量多的合并
1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 #include<algorithm> 5 #include<iostream> 6 7 using namespace std; 8 9 void setIO(const string& s) { 10 freopen((s + ".in").c_str(), "r", stdin); 11 freopen((s + ".out").c_str(), "w", stdout); 12 } 13 template<typename Q> Q read(Q& x) { 14 static char c, f; 15 for(f = 0; c = getchar(), !isdigit(c); ) if(c == '-') f = 1; 16 for(x = 0; isdigit(c); c = getchar()) x = x * 10 + c - '0'; 17 if(f) x = -x; 18 return x; 19 } 20 template<typename Q> Q read() { 21 static Q x; read(x); return x; 22 } 23 24 const int N = 100000 + 10; 25 int main() { 26 #ifdef DEBUG 27 freopen("in.txt", "r", stdin); 28 freopen("out.txt", "w", stdout); 29 #endif 30 31 static int du[N], mx[N]; 32 int T = read<int>(); 33 for(int cas = 1; cas <= T; cas++) { 34 memset(du, 0, sizeof du); 35 memset(mx, 0, sizeof mx); 36 int ans = 0; 37 int u, v, w, n = read<int>(); 38 for(int i = 1; i < n; i++) { 39 read(u), read(v), read(w); 40 du[u] += w, du[v] += w; 41 mx[u] = max(mx[u], w); 42 mx[v] = max(mx[v], w); 43 ans += w; 44 } 45 46 for(int u = 1; u <= n; u++) { 47 if((mx[u] << 1) <= du[u]) ans -= du[u] >> 1; 48 else ans -= du[u] - mx[u]; 49 } 50 51 printf("Case #%d: %d\n", cas, ans); 52 } 53 54 return 0; 55 }
原文出处http://www.cnblogs.com/showson/