Blue Bridge Cup--algorithm to improve the least squares of poor students into trees

Source: Internet
Author: User

First, the idea

Enumerates all spanning tree Benquan and values, Benquan and value sum for each enumeration, modifies the Benquan of all edges (Es[i].cost-sum * 1.0/(N-1)) 2, i.e. the numerator of the variance formula, and then runs the minimum spanning tree algorithm while recording the original weights of the edges and, if the "minimum variance" is calculated The Benquan value of the spanning tree is sum, so use this "minimum variance" to update the answer.

Second, the complexity of analysis

Time complexity: O (N * W * M * LOGM). N * W is the time to enumerate the edge weights and values. The Benquan and values are minimum 0 and the maximum is (N-1) * W.

Third, PS

The problem is said to be Blue Bridge Cup official data. There are 5 examples (2, 3, 4, 5, 6), always WA. So, the right code can only get 50 points.

Iv. Source Code

#include <bits/stdc++.h>using namespacestd;intN, M;Const intMAXN = -, MAXM =1010;Const DoubleINF = (1LL << -) *1.0; typedefstructEDGE0 {intu, V, oldcost; DoubleNewcost; BOOL operator< (EDGE0 e)Const {        returnNewcost <E.newcost; }    voidAssgin (int_u,int_v,int_cost) {u=_u; V=_v; Oldcost=_cost; }} Edge; Edge edges[maxm];template<classT> InlinevoidRead (T &x) {intT; BOOLFlag =false;  while((t = GetChar ())! ='-'&& (T <'0'|| T >'9')) ; if(T = ='-') flag =true, t =GetChar (); X= T-'0';  while(t = GetChar ()) >='0'&& T <='9') x = x *Ten+ T-'0'; if(flag) x =-x;}/** and check the set part*/intPAR[MAXN], RANK[MAXN];voidInit_ufind () { for(inti =0; i < MAXN; ++i) {Par[i]=i; Rank[i]=0; }}intUfind (intx) {returnx = = Par[x]? X:PAR[X] =Ufind (par[x]);}voidUniteintXinty) {x= Ufind (x), y =Ufind (y); if(x = = y)return; if(Rank[x] < rank[y]) par[x] =y; Else{Par[y]=x; if(Rank[x] = = Rank[y]) rank[x]++; }}BOOLSame (intXinty) {returnUfind (x) = =Ufind (y);}/** and check the set part*/DoubleKruscal (inttot) {    DoubleAVG = tot *1.0/(N-1);  for(inti =0; i < M; ++i) {edges[i].newcost= (Edges[i].oldcost *1.0-avg) * (Edges[i].oldcost *1.0-avg); } sort (edges, edges+M);    Init_ufind (); Doubleres =0; intIres =0;  for(inti =0; i < M; ++i) {Edge& e =Edges[i]; if(!Same (E.U, E.V))            {Unite (E.U, E.V); Res+=E.newcost; Ires+=E.oldcost; }    }    if(Ires = = tot)returnRES/(N-1); Else returnINF;}intMain () {#ifndef Online_judge freopen ("Input.txt","R", stdin);#endif    intT =1, A, B, C; intCOSTS[MAXM];  while(SCANF ("%d%d", &n, &m),! (N = =0&& M = =0)) {         for(inti =0; i < M; ++i) {read (a), read (b), read (c); EDGES[I].U= A, edges[i].v = b, edges[i].oldcost =C; Costs[i]=C; } sort (costs, costs+M); intMintot =0, Maxtot =0;  for(inti =0; I < N-1; ++i) Mintot + =Costs[i];  for(inti = M-1; i > M-n;--i) maxtot + =Costs[i]; DoubleAns =INF;  for(inttot = Mintot; Tot <= Maxtot; ++tot) {ans=min (ans, kruscal (tot)); } printf ("Case %d:%.2f\n", t++, ans); }    return 0;}

Blue Bridge Cup--algorithm to improve the least squares of poor students into trees

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.