Test instructions: Give a tree, identify a point, find all points to the point of the weight and maximum, the weight is the minimum value of all edge weights on the path.
With the magic of the collection, the road in accordance with the weights from the big to the small sort, and then with similar Kruskal method constantly join the edge. For a road to join, this road connects to the city X and Y,x where the set is a, y where the set is B, you can be sure that all the roads within a/b collection are larger than the current weight of the road. If you let set B join set A, which is to have the central city in collection A, then you can determine that the total weight of the two collections after merging is: the sum of the weights of a +b * The weight of the current road. The same is the case that the set B is added to set a, and the combination of the two weights is large.
1#include <cstdio>2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cmath>6#include <queue>7#include <map>8 using namespacestd;9 #defineMOD 1000000007Ten Const Doubleeps=1e-5; One #defineCL (a) memset (A,0,sizeof (a)) A #defineTS printf ("*****\n"); - Const intmaxn=200005; - intN,m,tt; the intF[MAXN]; - structEdge - { - inta,b,c; + voidinput () - { +scanf"%d%d%d",&a,&b,&c); A } at }EDGE[MAXN]; - intFindintx) - { - if(f[x]==-1)returnx; - returnf[x]=find (F[x]); - } in structNode - { to Long Longsum; + Long Longnum; - }NODE[MAXN]; the BOOLCMP (Edge A,edge B) * { $ returnA.c>B.C;Panax Notoginseng } - intMain () the { + inti,j,k; A #ifndef Online_judge theFreopen ("1.in","R", stdin); + #endif - while(SCANF ("%d", &n)! =EOF) $ { $ for(i=1; i<n;i++) Edge[i].input (); -Sort (edge+1, edge+n,cmp); - for(i=1; i<=n;i++) the { -node[i].num=1;Wuyinode[i].sum=0; thef[i]=-1; - } Wu for(i=1; i<n;i++) - { About inta1=edge[i].a; $ intA2=edge[i].b; - intt1=find (A1); - intT2=find (A2); - if(Node[t1].sum+ (Long Long) node[t2].num*edge[i].c<node[t2].sum+ (Long Long) node[t1].num*edge[i].c) A { +F[t1]=t2;//T2 as the selected point thenode[t2].num+=Node[t1].num; -node[t2].sum+= (Long Long) edge[i].c*Node[t1].num; $ } the Else the { thef[t2]=T1; thenode[t1].num+=Node[t2].num; -node[t1].sum+= (Long Long) edge[i].c*Node[t2].num; in } the } theprintf"%lld\n", Node[find (1)].sum); About } the}
Zoj 3659 The 37th session of the Acm/icpc Changchun field game E (and set)