Test instructions: Given the n points and n-1 edges, the maximum load of a to B is the minimum of the carrying capacity between A and B. At the center of a certain point, the maximum value of the sum of the carrying capacity is obtained.
Because the load between A and B is the minimum of the carrying capacity between them, the number of loads between two points is first sorted from large to small. Each time a merge has a A, a, b two sets, the amount of load between them (cost) is the current minimum, if B is merged to a, then the total carrying capacity of a is the total carrying capacity of a before a and A/b, the number of the median of the clothing set, namely Cost[a]=cost[a]+num[b]*cost.
#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespaceStd;typedefLong Longll;Const intinf=1000000000;Const intmaxn=200005;intF[maxn];ll NUM[MAXN],COST[MAXN];structbranch{intA; intb; ll cost;} BRA[MAXN];BOOLCMP (Branch a,branch b) {returnA.cost>B.cost;}intFind (intx) { if(x!=f[x]) f[x]=Find (f[x]); returnf[x];} ll Function (intN) { for(intI=0; i<n-1; i++) { intA=Find (BRA[I].A); intb=Find (BRA[I].B); if(cost[a]+num[b]*bra[i].cost>=cost[b]+num[a]*bra[i].cost) {F[b]=A; Cost[a]+=num[b]*Bra[i].cost; Num[a]+=Num[b]; } Else{F[a]=b; COST[B]+=num[a]*Bra[i].cost; NUM[B]+=Num[a]; } } returnCost[find (1)];}intMain () {//freopen ("In.txt", "R", stdin); intN; while(SCANF ("%d", &n)! =EOF) { for(intI=0; i<=n;i++) {F[i]=i; Num[i]=1; } memset (Cost,0,sizeof(cost)); for(intI=0; i<n-1; i++) scanf ("%d%d%i64d",&bra[i].a,&bra[i].b,&bra[i].cost); Sort (Bra,bra+n-1, CMP); printf ("%i64d\n", Function (n)); } return 0;}
HDU 4424 Conquer a New region and collection