Hiho 109th Week "Tower Defense Game"

Source: Internet
Author: User

Topic Link: Portal

To give you a tree, the root node is 1, each node in the tree has a cost value and income value (cost >= income value), to access a node to pay the cost, access to the end of the node to get the revenue value

The requirement to access the tree at the same time is ordered, that is, after accessing a node, it needs to access all of its child nodes to access its sibling nodes. Ask how much money we need at least to get access to all the nodes.

Topic idea: The feeling is a more interesting problem, first we can know by test instructions, visit the whole tree to pay the price is certain (total cost-gross income), set the price for X, after the visit we will get total revenue Y,

By test instructions know the amount of money we have after the visit y>=0, we take as little as possible, then Y should be as small as possible, we want to each node return the money as much as possible to spend, rather than stay in the hands.

Then only DFS is required to process the access order of the child nodes, which returns the optimal situation after each node has been accessed (with the least cost per node).

#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>#include<cstring>#include<stack>#include<cctype>#include<queue>#include<string>#include<vector>#include<functional>#include<Set>#include<map>#include<climits>#defineLson Root<<1,l,mid#defineRson Root<<1|1,mid+1,r#defineFi first#defineSe Second#definePing (x, y) ((x-y) * (x-y))#defineMST (x, y) memset (x,y,sizeof (x))#defineMCP (x, y) memcpy (x,y,sizeof (y))using namespacestd;#defineGamma 0.5772156649015328606065120#defineMOD 1000000007#defineINF 0x3f3f3f3f#defineN 100005#defineMAXN 10005typedef pair<int,int>Pii;typedefLong LongLL;intn,m;inta[10005],b[10005];vector<int>v[10005];structnode{intx,v; x cost value, v Revenue valueBOOL operator< (ConstNode&a)Const{        returnV>A.V; }}; Node DFS1 (intXintFA)    {Node T1; Vector<Node>v;  for(intU:v[x]) {        if(U==FA)Continue; T1=DFS1 (u,x);    V.push_back (t1);  } sort (V.begin (), V.end ()); Sort by revenue from big to smallintTemp1=a[x],temp2=b[x];///TEMP1 spend, temp2 revenue, to access current node takes a[x], access current node after revenue b[x] for(Node u:v) {Temp2-=u.x; if(temp2<0) {/// If the current income is not enough to cover the cost of the next node, you need more money Temp1-=Temp2; Temp2=0; } Temp2+=U.V; } t1.x=temp1;t1.v=Temp2; returnT1;}intMain () {intI,j,group,x,y,case=0;  while(SCANF ("%d", &n)! =EOF) {         for(i=1; i<=n;++i) scanf ("%d%d",&a[i],&B[i]);  for(i=1; i<n;++i) {scanf ("%d%d",&x,&y);            V[x].push_back (y);        V[y].push_back (x); } node Node=DFS1 (1,-1); printf ("%d\n", node.x); }    return 0;}

Hiho 109th Week "Tower Defense Game"

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.