nyoj-Cabling Problems

Source: Internet
Author: User

I used the prim algorithm and the Kruskal algorithm to submit a bit

From the code volume, prim algorithm is more concise, in terms of time complexity, Prim:o (n^2), Kruskal:o (MLOGM), Kruskal more excellent

Kruskal:

#include <iostream>#include<algorithm>#include<cstring>using namespacestd;Const intMax =2000000;structed{intu; intv; intW;}; Ed E[max];intF[max];intX[max];BOOLCMP (Ed a,ed b) {if(a.w<B.W)return true; Else      return false;}intGETF (intv) {    if(F[v] = =v)returnv; Else{F[v]=GETF (F[v]); returnF[v]; }}intMergeintUintv) {    intt1,t2; T1=GETF (F[u]); T2=GETF (F[v]); if(T1! =T2) {F[T2]=T1; return 1; }    return 0;}intMain () {intN; CIN>>N; intVn,en;  while(n--)    {              intsum =0; intCount =0; intMinele; CIN>> vn >>en;  for(inti =0; i < en; i++) Cin>> e[i].u >> e[i].v >>E[I].W;  for(inti =0; i < VN; i++) Cin>>X[i]; Minele= *min_element (x,x+vn); Sort (e,e+EN,CMP);//sort by weighted value                               for(inti =1; I <= vn; i++) F[i]=i;  for(inti =0; i < en; i++)               {                    if(merge (E[I].U,E[I].V)) {//if u and V are not connectedCount + +; Sum= Sum +E[I].W; }                    if(Count = = vn-1)                    {                           Break; }} cout<< sum + minele <<Endl; }       return 0;}

Prim

#include <iostream>#include<algorithm>#include<cstring>using namespacestd;Const intMax =550;Const intINF =999999999;intDis[max];intE[max][max];intBook[max];intX[max];intMain () {intN; intVn,en; CIN>>N;  while(n--) {cin>> vn >>en;  for(inti =1; I <= vn; i++)                  for(intj =1; J <= Vn; j + +) E[i][j]=inf; memset (book,0,sizeof(book)); intA,b,c; intsum =0; intCount =1;  for(inti =0; I < en;i++) {cin>> a >> b >>C; E[A][B]=C; E[b][a]=C; }                for(inti =0; i < VN; i++) Cin>>X[i]; intMinele = *min_element (x,x+vn);  for(inti =1; I <= vn;i++) Dis[i]= e[1][i]; book[1] =1;  while(Count <vn) {                            intMini =inf; intJ;  for(inti =1; I <= Vn;i + +)                               if(book[i]==0&&dis[i] <Mini) {Mini=Dis[i]; J=i; } Sum= Sum +Dis[j]; BOOK[J]=1; Count++;  for(inti =1; I <= vn; i++)                              if(Dis[i] >E[j][i])//update the vertex I to the shortest distance from the tree dis[i]=E[j][i]; } cout<< sum + minele <<Endl; }                 return 0; }          

nyoj-Cabling Problems

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.