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