"BZOJ-2435" road construction (tree-shaped DP?) ) DFS

Source: Internet
Author: User

2435: [Noi2011] Road construction time limit:10 Sec Memory limit:128 MB
submit:3115 solved:1002
[Submit] [Status] [Discuss] Description

There are n countries on planet W. For the economic development of their respective countries, they decided to
The construction of two-way roads makes connectivity between countries. But the kings of every country are so stingy that they only want to
To build exactly 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
Cost is difficult to calculate manually, the king decided to find someone to design a software, for a given construction plan,
To figure out the costs that are required. Please help the Kings design a software like this.

Input

The first line of input contains an integer n, representing the number of countries on planet W, the country from 1 to n
Number. Next n–1 describes road construction, where line I contains three integers ai, bi, and CI, table
The two-way road is constructed between AI and bi two countries with a length of CI.

Output

Output An integer that represents the total cost to build all roads.

Sample Input6
1 2 1
1 3 1
1 4 2
6 3 1
5 2 1
Sample Output20
HINT

n = 1,000,000 1≤ai, bi≤n
0≤ci≤10^6

Source

Day2

Solution

Look at the WC handout that seems to have this problem in the tree DP ...

However, the direct-violence DFS found the size[u of each node], then the other side of the size[v]=n-size[u]

Then you can accumulate the answer directly ...

pit point: pay attention to turn Longlong, (ABS middle also stronger turn longlong. Although unknown)

Code
#include <iostream>#include<cstring>#include<cmath>#include<cstdio>#include<algorithm>using namespacestd;intRead () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();}  while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;}#defineMAXN 1100000intNLong Longans;structedgenode{intNext,to, from, Len;} edge[maxn<<1];inthead[maxn],cnt;voidAddintUintVintW) {CNT++; Edge[cnt].to=V;EDGE[CNT].NEXT=HEAD[U];HEAD[U]=CNT;EDGE[CNT].LEN=W;EDGE[CNT]. from=u;}voidInsertintUintVintW) {Add (u,v,w); add (v,u,w);}intSIZE[MAXN];voidDFS (intNowintFaLong Longdis) {Size[now]=1;  for(intI=head[now]; I I=edge[i].next)if(edge[i].to!=FA) {DFS (Edge[i].to,now, (Long Long) Edge[i].len); Size[now]+=Size[edge[i].to]; } ans+=(Long Long) (Dis* (Long Long) ABS ((Long Long) (size[now]-(nSize[now] )));}intMain () {n=read ();  for(intU,v,w,i=1; i<=n-1; i++) U=read (), V=read (), w=read (), insert (U,V,W); DFS (1,0, 0LL); printf ("%lld\n", ans); return 0;}

At first think of the tree DP on the WC's handout, then consider the tree-shaped DP. Think, the size of this thing can be directly DFS out, directly calculate the answer AH? Is there a counter-example? So scratched a few sure seems not, and then tried to try a pleasant ac ... Smg

"BZOJ-2435" road construction (tree-shaped DP?) ) DFS

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.