[bzoj] 3754: Tree之最小方差树 发表于 Apr 6 2015 | 分类于 bzoj | 题意rt 分析枚举方差即可手抖毁一生 代码123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110/****************************************\* Author : ztx* Title : [bzoj] 3754: Tree之最小方差树* ALG : 枚举* CMT : * Time :\****************************************/#include <cstdio>#define Rep(i,l,r) for(i=(l);i<=(r);i++)#define Rev(i,r,l) for(i=(r);i>=(l);i--)typedef long long ll ;typedef double db ;int CH , NEG ;template <typename TP>inline void read(TP& ret) { ret = NEG = 0 ; while (CH=getchar() , CH<'!') ; if (CH == '-') NEG = true , CH = getchar() ; while (ret = ret*10+CH-'0' , CH=getchar() , CH>'!') ; if (NEG) ret = -ret ;}template <typename TP>inline void reads(TP& ret) {truewhile (ret=getchar() , ret<'!') ;truewhile (CH=getchar() , CH>'!') ;}#include <algorithm>#include <cstring>#include <cmath>#define maxn 2010LL#define maxm 2010LL#define infi 10000000.0F#define sqr(x) ((x)*(x))struct EDGE { int u , v , w ; db ww ; } e[maxm] ;inline bool cmp(const EDGE& a , const EDGE& b) { return a.w < b.w ; }inline bool cmp2(const EDGE& a , const EDGE& b) { return a.ww < b.ww ; }int par[maxn] ;inline int GetAnc(int u) { return par[u] ? par[u]=GetAnc(par[u]) : u ; }inline bool Union(int u , int v) {trueu = GetAnc(u) , v = GetAnc(v) ;trueif (u == v) return false ;truepar[u] = v ; return true ;}int n , m , sum , sum0 , cnt ;db now = 0 , ave = 0 , ans = 0 ;void GST() {int i ;truememset(par , 0 , sizeof par) ;trueave = (db)sum0/(db)(n-1) ;trueRep (i,1,m) {truetruee[i].ww = sqr((db)e[i].w-ave) ;true}truestd::sort(e+1,e+m+1,cmp2) ;truesum = cnt = 0 , now = 0 ;trueRep (i,1,m) {truetrueif (Union(e[i].u,e[i].v)) {truetruetruesum += e[i].w ;truetruetruenow += e[i].ww ;truetruetrueif (++cnt == n-1) break ;truetrue}true}trueif ((cnt!=n-1) || (sum!=sum0)) return ;truenow /= (db)(n-1) ;trueif (now < ans) ans = now ;}int main() {int i , smin , smax ;true#define READtrue#ifdef READtruetruefreopen("tree.in" ,"r",stdin ) ;truetruefreopen("tree.out","w",stdout) ;true#endiftrueread(n) , read(m) ;trueif (n <= 1) { puts("0.0000") ; goto END ; }trueRep (i,1,m)truetrueread(e[i].u) ,truetrueread(e[i].v) ,truetrueread(e[i].w) ;truestd::sort(e+1,e+m+1,cmp) ;truesmin = smax = 0 ;trueRep (i,1,n-1)truetruesmin += e[i].w ,truetruesmax += e[m-i+1].w ;truetrue//smax += e[n-i+1].w ; 剁手QAQtrueans = infi ;trueRep (sum0,smin,smax)truetrueGST() ;trueif (ans == infi) puts("0.0000") ;trueelse printf("%.4lf\n", sqrt(ans)) ;trueEND:;true#ifdef READtruetruefclose(stdin) ; fclose(stdout) ;true#elsetruetruegetchar() ; getchar() ;true#endiftruereturn 0 ;}/*3 31 2 12 3 23 1 3*/