"NOI2011", "P1308" road construction

Source: Internet
Author: User

This question is too water, why not my this session of Noi (╯‵-′) ╯︵┻━┻

Original question:

There are n countries on planet W. For the economic development of their respective countries, they decided to build a two-way road between countries to make the country connect. But the kings of every country are stingy, and they are only willing to build exactly the n–1 two-way road.
The construction of each road costs a fee equal to the absolute value of the road length multiplied by the number of countries on both sides of the road. For example, in the dotted line, there are 2 or 4 countries at each end of the road, and if the road length is 1, the cost is 1x|2–4|=2. The number in the circle in the figure represents the country's number.

As the number of countries is very large, there are many ways to build roads, while the construction of each of these programmes
The costs were difficult to calculate manually, and the Kings decided to find someone to design a software that would calculate the cost for a given construction plan. Please help the Kings design a software like this

n<=1000000,ci<=1000000

Just started to read the wrong question, thought is to give any two countries distance, and then survive into a tree ...

But the tree is directly given out, direct evaluation can not maintain the optimal value, is a simulation ...

So the problem should be a tree-shaped recursion instead of a tree.

Choose a point to make a tree, a little observation can be found that each point and its father, the right side of the weight is |n-size[x]-size[x]|*value[x] (value[x] means the length of the edge between X and dad)

And then I'm asking for size.

The data is very large, so DFS will blow up (Linux or manual open stack does not explode?) ), so use BFS

First DFS again, the tree is built, and then backward to the queue to traverse again, because the BFS is a layer of push, so the anti-traversal queue to ensure that the first son, in the father, and then you can update the

Finally from the 2-n according to the above formula ↑ The evaluation of accumulation can (I set the root node to 1, the root node is not updated because the root node does not have a father)

Note that it will explode int, if the result of the multiplication will explode int then the multiply of the two number must have at least one is longlong, it is a good choice to set the return value of the absolute function to Longlong.

Code:

1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5#include <cmath>6 using namespacestd;7 intRead () {intz=0, mark=1;CharCh=GetChar ();8      while(ch<'0'|| Ch>'9'){if(ch=='-') mark=-1; Ch=GetChar ();}9      while(ch>='0'&&ch<='9') {z= (z<<3) + (z<<1) +ch-'0'; Ch=GetChar ();}Ten     returnz*Mark; One } AInlineLong LongJue (Long LongXLong LongY) {return(x>y)? X-y:y-x;}//pay attention to the absolute value also Longlong - structddd{intNext,y,value;} e[2100000];intlink[1100000],ltop=0; -InlinevoidInsertintXintYintZ) {e[++ltop].next=link[x]; Link[x]=ltop;e[ltop].y=y;e[ltop].value=Z;} the structdcd{intChild,brother,father,value,size;} tree[1100000]; -InlinevoidInsert_tree (intXintYintZ) {tree[y].brother=tree[x].child;tree[x].child=y;tree[y].father=x;tree[y].value=Z;} - intN; - intdui[1100000],tou=0; + voidGet_tree () { -dui[tou=1]=1; tree[1].father=tree[1].value=0; +      for(intk=1; k<=tou;k++) A          for(intI=link[dui[k]];i;i=e[i].next)if(e[i].y!=tree[dui[k]].father) { at Insert_tree (dui[k],e[i].y,e[i].value); -dui[++tou]=e[i].y; -         } -      for(intk=tou;k>=1; k--){ -Tree[dui[k]].size=1; -          for(intI=link[dui[k]];i;i=e[i].next)if(e[i].y!=tree[dui[k]].father) intree[dui[k]].size+=tree[e[i].y].size; -     } to } + intMain () {//freopen ("ddd.in", "R", stdin); -Cin>>N; the     int_left,_right,_value; *      for(intI=1; i<n;i++){ $_left=read ();  _right=read (); _value=read ();Panax Notoginseng Insert (_left,_right,_value); Insert (_right,_left,_value); -     } the Get_tree (); +     Long Longans=0; A      for(intI=2; i<=n;i++) Ans+=jue (n-tree[i].size,tree[i].size) *Tree[i].value; thecout<<ans<<Endl; +     return 0; -}
View Code

"NOI2011", "P1308" road construction

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.