B-networking POJ-1287

Source: Internet
Author: User
Tags cmath

Main topic:

To find the minimum spanning tree. Enter a n,k. n indicates that there are n points, followed by k lines, each line entered three digits a,b,c, meaning: the distance between AB is C. End input when n=0.

n<=50,k<=100

Problem Solving Ideas:

Set of templates. Note There may be duplicate paths. such as 1 2 6, 2 1 8. 1 2 6 will be taken at this time.

1#include <iostream>2#include <vector>3#include <map>4#include <string>5#include <queue>6#include <stack>7#include <algorithm>8#include <Set>9 Ten#include <cstdio> One#include <cstring> A#include <cmath> -#include <cstdlib> - using namespacestd; the  - Const intinf=0x3f3f3f3f; - Const intSize= -;///I don't know how big it is to drive a bigger one. -  + intId[size]; - intSz[size]; + intM,n; A  at structnode{ -     intstart; -     intend; -     intVal; -  -}edge[10000];///emmmm in voidClear () - { to      for(intI=0; i<=n;i++) +     { -id[i]=i;sz[i]=1; the     } * } $ intFindintx)Panax Notoginseng { -     if(X!=id[x]) id[x]=find (Id[x]); the     returnId[x]; + } A  the voidUnintPintq) + { -p=find (p); $q=find (q); $     if(P==Q)return ; -     if(sz[p]<Sz[q]) -id[p]=find (q); the     Else -     {Wuyi         if(sz[p]==Sz[q]) thesz[p]++; -id[q]=find (p); Wu     } - } About  $ intKruskal () - { -     intsum=0; -      for(intI=0; i<m;i++) A     { +         if(Find (edge[i].start)! =find (edge[i].end)) the         { - un (edge[i].start,edge[i].end); $sum+=Edge[i].val; the         } the     } the     returnsum; the  - } in  the BOOLCMP (node A,node b) the { About     returna.val<B.val; the } the intMain () the { +      while(cin>>N) -     { the         if(n==0) Break;BayiCin>>m; the Clear (); the         intA,b,value; -          for(intI=0; i<m;i++) -         { theCin>>a>>b>>value; theedge[i].start=A; theEdge[i].end=b; theEdge[i].val=value; -         } theSort (edge,edge+m,cmp); theCout<<kruskal () <<Endl; the     }94     return 0; the}
Kruskal Algorithm
1#include <iostream>2#include <vector>3#include <map>4#include <string>5#include <queue>6#include <stack>7#include <Set>8 9#include <cstdio>Ten#include <cstring> One#include <cmath> A#include <cstdlib> - using namespacestd; -  the Const intinf=0x3f3f3f3f; - Const intSize= -; -  - intDis[size]; + intGraph[size][size]; - intn,m; + intVis[size]; A  at intPrim () - { -memset (Vis,0,sizeof(Vis)); -     intindex; -     intsum=0; -      for(intI=1; i<=n;i++)///starting point is 1 indis[i]=graph[i][1]; -vis[1]=1; to      for(intI=2; i<=n;i++) +     { -         intminn=INF; the          for(intj=1; j<=n;j++) *             if(!vis[j]&&dis[j]<Minn) $         {Panax Notoginsengminn=dis[j];index=J; -         } thevis[index]=1; +sum+=Minn; A          for(intj=1; j<=n;j++) the             if(dis[j]>graph[j][index]&&!Vis[j]) +dis[j]=Graph[j][index]; -     } $     returnsum; $ } - intMain () - { the       while(cin>>N) -     {Wuyi         if(n==0) Break; theCin>>m; -memset (Graph,inf,sizeof(graph)); Wu         intA,b,value; -          for(intI=0; i<m;i++) About         { $Cin>>a>>b>>value; -             if(Graph[a][b]>value)///just keep repeating. -graph[a][b]=graph[b][a]=value; -         } ACout<<prim () <<Endl; +     } the     return 0; -}
Prim Algorithm

B-networking POJ-1287

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.