The coordinates of the node are given and the edges that have been built are to output the edges added to the MST (the built-in edges are not output)
The node number starts at 1.
Note that there is only one set of data that cannot be entered with multiple sets of input otherwise it will time out (in this case the pit is miserable Orz)
Sample Input
9
1 5
0 0
3 2
4 5
5 1
0 4
5 2
1 2
5 3
3
1 3
9 7
1 2
Sample Output
1 6
3 7
4 9
5 7
8 3
1# include <iostream>2# include <cstdio>3# include <cstring>4# include <algorithm>5# include <cmath>6# define LLLong Long7 using namespacestd;8 9 Const intinf=0x3f3f3f3f;Ten Const intmaxn=760; One BOOLVIS[MAXN]; A DoubleLOWC[MAXN]; - intN; - DoubleCOST[MAXN][MAXN]; the intPRE[MAXN]; - - structPoin - { + intx; - inty; + }P[MAXN]; A at voidPrim () - { -memset (Vis,false,sizeof(Vis)); -vis[1]=true; - for(intI=2; i<=n;i++) - { inlowc[i]=cost[1][i]; -Pre[i] =1 ; to } + - for(intI=2; i<=n;i++) the { * DoubleMinc=INF; $ intp=-1;Panax Notoginseng for(intj=1; j<=n;j++) - if(!vis[j]&&minc>Lowc[j]) the { +Minc=Lowc[j]; Ap=J; the } + if(Minc! =0) - { $printf"%d%d\n", pre[p],p); $ } - if(p = =-1) - return ; thevis[p]=true; - for(intj=1; j<=n;j++)Wuyi if(!vis[j]&&lowc[j]>Cost[p][j]) the { -lowc[j]=Cost[p][j]; Wupre[j]=p; - } About } $ return ; - } - - intMain () A { + the //freopen ("In.txt", "R", stdin); -scanf"%d", &n); $ intI, J; the for(i =1; I <= N; i++) thescanf"%d%d", &p[i].x, &p[i].y); the for(i =1; I <= N; i++) the { -Cost[i][i] =INF; in for(j = i+1; J <= N; J + +) the { the Doublet = sqrt ((Double) (p[i].x-p[j].x) * (p[i].x-p[j].x) + (P[I].Y-P[J].Y) * (P[I].Y-p[j].y)) ; AboutCOST[I][J] =T; theCost[j][i] =T; the } the } + - intm, u, v; thescanf"%d", &m);Bayi while(m--) the { thescanf"%d%d", &u, &v); -COST[U][V] =0 ; -Cost[v][u] =0 ; the } the Prim (); the the - return 0 ; the}
View Code
POJ 1751 The newly added edge of the output MST