This is a variant of the minimal spanning tree (MST) problem.
Add a vis Array Based on the template of my minimal spanning tree prim algorithm (you need the template to click the http://blog.csdn.net/rising_fallmoon/article/details/9819187) to identify whether the node has been added to the Collection T. Here, the min_dis of a node cannot be used as whether to add the node to T. Because the connected edge is given in the question and its weight is set to 0, an array must be added for determination.
Another note is that here prim does not necessarily need to execute loop N-1 times, also because Edge permission Initialization is 0. Terminating the cycle in time can slightly improve the efficiency.
My problem-solving code is as follows:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <string>#include <algorithm>using namespace std;#define Distance double#define INF 1000000#define MAXN 750double x[MAXN],y[MAXN];Distance dis[MAXN][MAXN];Distance min_dis[MAXN];//int nearest_v[MAXN];int vis[MAXN];Distance Prim(int v0, int N){//initfor(int i=0; i<N; i++){min_dis[i] = dis[i][v0];//nearest_v[i] = v0;}min_dis[v0] = 0;memset(vis,0,sizeof(vis));vis[v0] = 1;Distance total_dis = 0;for(int k=1; k<N; k++){Distance md = INF;int nv = v0;for(int i=0; i<N; i++) if(!vis[i]){if(md > min_dis[i]) {md = min_dis[i];nv = i;}}total_dis += md;min_dis[nv] = 0;vis[nv] = 1;for(int i=0; i<N; i++) if(!vis[i]){if(min_dis[i] > dis[i][nv]){min_dis[i] = dis[i][nv];//nearest_v[i] = nv;}}int ok = 0;for(int i=0; i<N; i++) if(min_dis[i]!=0) { ok=1; break; }if(!ok) break;}return total_dis;}int main(){int N,M;while(cin >> N){for(int i=0; i<N; i++){scanf("%lf %lf",&x[i],&y[i]);for(int j=0; j<=i; j++) dis[i][j]=dis[j][i]=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])); }cin >> M;int na,nb;for(int i=0; i<M; i++){scanf("%d %d",&na,&nb);dis[na-1][nb-1]=dis[nb-1][na-1]=0;}printf("%.2lf\n", Prim(0,N));}return 0;}
The attached questions are as follows:
Creating new buildings are under construction on the campus of the University of Waterloo. The University has hired bricklayers, electricians, plumbers, and a computer programmer. A computer programmer? Yes, you have been hired to ensure that each building is
Connected to every other building (directly or indirectly) through the campus network of communication cables.
We will treat each building as a point specified by an X-coordinate and a Y-coordinate. each communication cable connects exactly two buildings, following a straight line between the buildings. information travels along a cable in both directions. cables
Can freely cross each other, but they are only connected together at their endpoints (at buildings ).
You have been given a campus map which shows the locations of all buildings and existing communication cables. you must not alter the existing cables. determine where to install new communication cables so that all buildings are connected. of course,
University wants you to minimize the amount of new cable that you use.
Fig: University of Waterloo campus
Input
The input file describes several test case. The description of each test case is given below:
The first line of each test case contains the number of buildingsN (1 <= n <= 750). The buildings are labeled from1ToN. The nextNLines giveXAndYCoordinates
Of the buildings. These coordinates are integers with absolute values at most10000. No two buildings occupy the same point. After that there is a line containing the number of existing CablesM (0 <= m <= 1000)FollowedMLines
Describing the existing cables. Each cable is represented by two integers: The building numbers which are directly connected by the cable. There is at most one cable directly connecting each pair of buildings.
Output
For each set of input, output in a single line the total length of the new cables that you plan to use, rounded to two decimal places.
Sample Input
4
103 104
104 100
104 103
100 100
1
4 2
4
103 104
104 100
104 103
100 100
1
4 2
Sample output
4.41
4.41
(Problem-setters: G. kemkes & G. V. Cormack, CS Dept,University of Waterloo)