Dfs/dp
Originally thought is a silly problem, but kneeling for a long time ... Always re ...
Direct DFS just fine ... x->y val=c:ans+=abs (n-size[y]-size[y]) *c;
But why is it always re?
Later changed to DFS returns the current node size is over ... After the ...
The 100W node is also a stack under Linux. Sad
1 /**************************************************************2 problem:24353 User:tunix4 language:c++5 result:accepted6 time:5292 Ms7 memory:73348 KB8 ****************************************************************/9 Ten //Bzoj 2435 One#include <vector> A#include <cstdio> -#include <cstring> -#include <cstdlib> the#include <iostream> -#include <algorithm> - #defineRep (i,n) for (int i=0;i<n;++i) - #defineF (i,j,n) for (int i=j;i<=n;++i) + #defineD (i,j,n) for (int i=j;i>=n;--i) - #definePB Push_back + using namespacestd; AInlineintGetint () { at intv=0, sign=1;CharCh=GetChar (); - while(ch<'0'|| Ch>'9'){if(ch=='-') sign=-1; Ch=GetChar ();} - while(ch>='0'&&ch<='9') {v=v*Ten+ch-'0'; Ch=GetChar ();} - returnv*Sign ; - } - Const intn=1001000, inf=~0u>>2; intypedefLong LongLL; - /******************tamplate*********************/ to + intto[n<<1],nxt[n<<1],head[n],cnt,l[n<<1]; - voidAddintXintYintz) { theTo[++cnt]=y; NXT[CNT]=HEAD[X]; head[x]=cnt; l[cnt]=Z; *To[++cnt]=x; Nxt[cnt]=head[y]; head[y]=cnt; l[cnt]=Z; $ }Panax Notoginseng intN,s[n]; - LL ans; the + intDfsintXintFA) { As[x]=1; the for(intI=head[x];i;i=Nxt[i]) + if(to[i]!=FA) { -s[x]+=DFS (to[i],x); $ans+= (LL) ABS (n-s[to[i]]*2)*L[i]; $ } - returnS[x]; - } the intMain () { - #ifndef Online_judgeWuyiFreopen ("2435.in","R", stdin); theFreopen ("2435.out","W", stdout); - #endif Wun=getint (); -F (I,2, N) { About intX=getint (), Y=getint (), z=getint (); $ Add (x, y, z); - } -Dfs1,0); -cout <<ans<<Endl; A return 0; +}
View Code 2435: [Noi2011] Road construction time limit:10 Sec Memory limit:128 MB
submit:2477 solved:753
[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 Input
6
1 2 1
1 3 1
1 4 2
6 3 1
5 2 1
Sample Output
20
HINT
n = 1,000,000 1≤ai, bi≤n
0≤ci≤10^6
Source
Day2
[Submit] [Status] [Discuss]
"Bzoj" "2435" "NOI2011" road construction