Read the original question
Here are the coordinates of a few (<= 100) Small Islands. Then, you can connect all the islands to the bridge to minimize the cost. There are additional restrictions: A bridge can be built only when the distance between islands is greater than or equal to 10, or less than or equal to 1000.
It is probably because no bridge is needed within 10 meters, and a bridge cannot be built over a kilometer. Haha.
Obviously, this is an MST (Minimum Spanning Tree) question. It seems that someone has used it and checked the AC.
Minimum Spanning Tree Algorithm
Overview
The common algorithms of the Minimum Spanning Tree are two kruskal and prim algorithms. Both do not stop merging operations. However, in a word, the difference between the two is: kruskal ---- merge edge; prim ---- merge point
The original questions about kruskal are as follows:
Hdu1301hdu1879
Hdu3371prim Algorithm
First, paste a Wikipedia link "prim algorithm ". Next I will describe it in discrete mathematics.
If graph G = (V, E) is set, all the node sets are V, and there is an empty set U. The basic idea is:
Determine a starting point at will first. Set this point to v and add it to the set U. Observe the edges connected to the midpoint of U and find the shortest edges. Add the vertex at the other end of the shortest edge to the set U. Continue with Step 3. Know V-U = 0. That is, all vertices are added to U. Simple implementation of AC code
This is the most general implementation solution. Almost template implementation code may be mentioned in any data structure book.
Void prim () {double sum = 0, lowcast [105] = {0}; int count = 1; for (int I = 0; I
Lowcast [j] & lowcast [j]) {min = lowcast [j]; k = j;} if (k! = 105) {sum + = lowcast [k]; lowcast [k] = 0; count ++;} for (int j = 0; j
Explanation
The two-dimensional array d stores the distance between all islands. First, the starting point is node 0.
For (int I = 0; I
The lowcast array stores the closest distance from a point in the set U to each node in the V-U. At the beginning, the set U contains only node 0, so the lowcast array stores the distance from node 0 to each node.
For (int I = 0; I
This is the main part of the prim algorithm. The initialization min is used to save the length of the edge with the minimum distance of the vertex in the set U. K is the node name of the other end used to save the shortest edge.
for(int j=0;j
lowcast[j]&&lowcast[j]){min = lowcast[j];k = j;}
It is also easy to understand here, that is, the length of the shortest side and the name of the other end of the shortest side are constantly updated. Note that when lowcast [j] = 0, it indicates the node.
JYou have joined the set U.
if(k!=105){sum+=lowcast[k];lowcast[k]=0;count++;}If k! = 105, indicating that the shortest side is found. Therefore, add the length of this edge lowcast [k] to the sum, and mark that the k has been added to the set U, that is, lowcast [k] = 0. Count is used to record the number of points that have been added to the set U.
For (int j = 0; j
Because vertices in set U may have increased, the closest distance from vertices in set U to points in V-U is also updated. This operation is called
"Relaxation operation". There are many applications in graph theory.
Complete AC code
# Include
Using namespace std; # include
Const double INF = 0x3f3f3f * 1.0; struct node {int x; int y ;}; double d [105] [105]; int c; // number of islands void prim () {double sum = 0, lowcast [105] = {0}; int count = 1; for (int I = 0; I
Lowcast [j] & lowcast [j]) {min = lowcast [j]; k = j;} if (k! = 105) {sum + = lowcast [k]; lowcast [k] = 0; count ++;} for (int j = 0; j
> T; while (t --) {node n [105]; cin> c; for (int I = 0; I
> N [I]. x> n [I]. y; for (int I = 0; I
1000.0) d [j] [I] = d [I] [j] = INF; elsed [j] [I] = d [I] [j] = dist ;} prim ();} return 0 ;}
Other annotations
const double INF=0x3f3f3f3f*1.0;
A double constant that represents infinity. For more information about how to select this value, see how to set infinite constants in programming.
When calculating the distance between the island and the island.
if(i==j) { d[i][j]=0; continue; }It indicates that if it is I = j, it is directly set to 0. In the prim algorithm, the corresponding lowcast will also be 0 and will be ignored. I cannot set it to INF here. In my implementation scheme, INF will cause the AC to fail. The specific solution is analyzed.