Luo Gu 1265prim minimum spanning tree algorithm

Topic links: https://www.luogu.com.cn/problem/P1265

Prim with minimum spanning tree algorithm dijkstra algorithm is very like, the point is to be divided into two sets, one is already in the collection of points spanning tree, is a collection point has not joined spanning tree. Initially select a point into the set {V1}, and from {V} - Select {V1} point set to {V1} into the set of points {V1} shortest distance point, so iterative manner until all the points have been visited too. In fact, this is a connected component of the merger process, each time selecting a minimum combined cost of the merger, the optimal solution ultimately derived from the local optimal solution overall, as strict proof of the correctness of the minimum spanning tree algorithm, see my another blog, I am a chicken dish, but also there is an error, please correct me.

code show as below:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef unsigned int ui;
 4 typedef long long ll;
 5 typedef unsigned long long ull;
 6 #define pf printf
 7 #define mem(a,b) memset(a,b,sizeof(a))
 8 #define prime1 1e9+7
 9 #define prime2 1e9+9
10 #define pi 3.14159265
11 #define lson l,mid,rt<<1
12 #define rson mid+1,r,rt<<1|1
13 #define scand(x) scanf("%llf",&x) 
14 #define f(i,a,b) for(int i=a;i<=b;i++)
15 #define scan(a) scanf("%d",&a)
16 #define mp(a,b) make_pair((a),(b))
17 #define P pair<int,int>
18 #define dbg(args) cout<<#args<<":"<<args<<endl;
19 #define inf 0x3f3f3f3f
20 const int maxn=1e6+10;
21 int n,m,t;
22 inline int read(){
23     int ans=0,a = ;
124     char ch=getchar();
25     while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}
26     while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
27     return ans*w;
28 }
29 struct node{
30     int x,y;
31 }e[maxn];
32 double ans;
33 double d[maxn];
34 int vis[maxn];
35 double dis(int i,int j)
36 {
37     int x=e[i].x,y=e[i].y,xx=e[j].x,yy=e[j].y;
38     return sqrt((double)(x-xx)*(x-xx)+(double)(y-yy)*(y-yy));
39 }
40 void prim()
41 {
42     mem(vis,0);
43     f(i,1,n)d[i]=inf; 
44     d[1]=0;
45     F (I, . 1 , n) // iterations is n times 
46 is      {
 47          int POS;
 48          Double tmp;
 49          tmp = INF, POS = - . 1 ;
 50          F (J, . 1 , n)
 51 is          {
 52 is                  IF ( ! VIS [J] && D [J] < tmp)
 53 is                  {
 54 is                      tmp = D [J], J = POS; // find the minimum set point and a number of side length 
55                  }
 56 is          }
 57 is          IF (tmp == INF) return ;
 58         VIS [POS] = . 1 ;
 59          ANS + = tmp;
 60          F (J, . 1 , n-) // just marked points remaining points relaxation operation 
61 is          {
 62 is              IF (! VIS [J]) 
 63 is              D [J ] = min (D [J], DIS (POS, J));
 64          }
 65      } 
 66  }
 67  int main ()
 68  {
 69      // The freopen ( "input.txt", "R & lt", stdin);
 70      / / The freopen ( "output.txt", "W", stdout); 
71 is      STD :: :: iOS sync_with_stdio ( to false );
72      n=read();
73      f(i,1,n)
74      {
75          e[i].x=read(),e[i].y=read();
76     } 
77      prim();
78      pf("%.2lf",ans);
79 } 

 

 

Guess you like

Origin www.cnblogs.com/randy-lo/p/12571635.html