Title Link: http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1252
Idea: Consider each side's damage to the player
Assuming that the connected nodes are u,v, the destructive force is p[u] and P[v]
Hypothesis P[u]>p[v]
Now consider u,v the deletion order, if the first delete u, this edge of the player's damage, is p[v], first delete V, the damage is p[u]
So obviously for each side, we have to delete the right value of the big, to the best
How can you delete the largest one for each edge, then delete the weight by decreasing it?
So ret=σ (min (p[u],p[v]))
Complexity O (N)
#include <cstdio>using namespacestd;Const intn=1e5+5;intP[n];intMain () {intN; while(~SCANF ("%d",&N)) { for(intI=1; i<=n;++i) scanf ("%d",&P[i]); intret=0; for(intI=1; i<n;++i) { intu,v; scanf ("%d%d",&u,&v); RET+=min (p[u],p[v]); } printf ("%d\n", ret); } return 0;}
View Code
Xtoj 1252 Defense Tower Greedy