Test instructions: Chinese title
Idea: Prim realization, because there are N (n-1)/2 edge, is already saturated edge, prim more appropriate.
(1) Set point 1 to browse, point 1 can reach each other point, so use the Low[i] array to record the current reach I point minimum length.
(2) in the low array to find the point that has not been browsed, and the nearest, set to browse, remember that the store is POS.
(3) Update the low array with all points reachable from the Pos point, so that the distance from the browsed point to point I is shortest to low[i].
(4) Return to 2 to continue execution until all the points have been browsed.
In the 2nd step, you can record the minimum path length by the way.
1#include <bits/stdc++.h>2 using namespacestd;3 Const intn= the;4 intV[n][n];//Rights5 intVis[n];6 intLow[n];//to the minimum right of each point7 8 9 intPrimintN//Primm AlgorithmTen { Onememset (Vis,0,sizeof(Vis)); A intpos=vis[1]=1;//starting from point 1 - for(intI=1; i<=n; i++)if(v[1][i]>0) low[i]=v[1][i];//at present to each point of the smallest right - intans=0; the for(intI=1; i<n; i++)//Take the other n-1. - { - intbig=Long_max; - for(intj=1; j<=n; J + +)//The least right edge, and the corresponding point + { - if(!vis[j] && low[j]<big) + { Apos=J; atbig=Low[j]; - } - } -Ans+=big;//Long Statistical path -vis[pos]=1; - for(intj=1; j<=n; J + +)//update weights to each point in if(!vis[j]) low[j]=min (low[j],v[pos][j]); - } to returnans; + } - the * $ Panax Notoginseng intMain () - { theFreopen ("Input.txt","R", stdin); + intN, a, B, t; A while(SCANF ("%d", &n),n>0) the { + intup=n* (n1)/2;//Super Dense map - for(intI=0; i<up; i++) $ { $scanf"%d%d%d",&a,&b,&t); -v[a][b]=v[b][a]=T; - } theprintf"%d\n", Prim (n)); - }Wuyi return 0; the}
AC Code
HDU 1233 or unblocked project (minimum spanning tree, prim)