Xtoj 1252 Defense Tower Greedy

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.