Prim algorithm POJ1258

Source: Internet
Author: User

http://poj.org/problem?id=1258

This problem is the simplest one,,,,

1#include <stdio.h>2#include <iostream>3#include <string.h>4#include <algorithm>5 using namespacestd;6 Const intn= the;7 #defineM 0x3f3f3f3f///Set Infinity number m, when the distance between two points is not connected to M8 intMa[n][n],low[n],vis[n];///Ma records the length of the road, low record the shortest path, vis to determine whether this9 intN;Ten intPrim () { One inti,j,pos,mi,result=0; Amemset (Vis,0,sizeof(Vis)); -vis[1]=1;///traverse from 1 onwards -pos=1;///record the current node. the  -  -  for(i=1; i<=n;i++) -     if(I!=pos) low[i]=ma[pos][i];///, the initial pos equals 1, looking for the shortest distance in the nodes that can be passed around the POS and stored in low +  -  +  for(i=1; i<n;i++) {///Traverse n-1 Times AMi=M; at      for(j=1; j<=n;j++){ -         if(vis[j]==0&&mi>Low[j]) { -Mi=Low[j]; -pos=J; -  -         } in     } -result+=mi; tovis[pos]=1; +      for(j=1; j<=n;j++) {///find the shortest distance around POS -         if(vis[j]==0&&low[j]>Ma[pos][j]) thelow[j]=Ma[pos][j]; *     } $ }Panax Notoginseng returnresult; - } the intMain () + { A     intI,v,j,ans; the      while(~SCANF ("%d",&N)) { +memset (Ma,m,sizeof(MA)); -          for(i=1; i<=n;i++){ $              for(j=1; j<=n;j++){ $scanf"%d",&v); -ma[i][j]=ma[j][i]=v; -  the             } -         }Wuyians=Prim (); thecout<<ans<<Endl; -     } Wu     return 0; -}
View Code

Prim algorithm POJ1258

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.