Prim algorithm of farm issues and Krukal comparison algorithm

Problem Description:

Farmer John was elected mayor of their town! One of his campaign promises was in town to establish the Internet and connect to all farms. Of course, he needs your help. John has given his farm and arranged for a high-speed network lines, he wanted to share this line to other farms. In order to spend the least, he wanted to lay the shortest optical fiber to connect all the farms. You will get a list of connection costs between each farm, you must be able to connect all the farms and find the shortest program with optical fiber. The distance between each of the two farms will not be more than 100,000 

 

Entry

The first line: the number of farms, N (3 <= N <= 100). 

 

End of the second line ..: The next line contains a N * N matrix representing the distance between each farm. In theory, they are N lines, each consisting of N space-separated number composed, in fact, they each line is restricted to 80 characters, so some guilds followed by other lines. Of course, the diagonal will be zero, because the line does not make sense from the i-th farm to distance itself In this problem. 

 

Export

Only one output, each of the optical fiber is connected to the farm and minimum length.

 

Sample input

4

0    4   9   21

4    0   8   17

9    8   0   16

21  17  16   0

 

Sample Output

28

 

 

Kruskal:

The initial number of edges is zero minimum spanning tree, for each iteration selects a minimum cost to meet the conditions of the edge, is added to the set of edges in the minimum spanning tree.

1. FIG all sides by the cost of small to large;

2. FIG. N independent n vertices as a forest of trees;

3. Select small to large weight edges, two vertices connected by edges ui selected, viui, vi, should belong to two different trees, it becomes a minimum spanning tree edge, these two trees and the combined as a tree.

4. Repeat (3) until all vertices in the tree or there are n-1 until the edges.

 

Prim:

Selecting a minimum cost for each iteration corresponding to the edge point, is added to the minimum spanning tree. The algorithm starts from a certain vertex s, gradually grow to cover all the vertices of the entire communication network.

1. The set of all vertices is VV of FIG; make initial set u = {s}, v = V-uu = {s}, v = V-u;

2. In the two sides of set u, vu, v can be composed of, selecting a minimum cost edge (u0, v0) (u0, v0), was added to the minimum spanning tree, and incorporated into the set in u v0v0 .

3. Repeat the above steps until there are a minimum spanning tree until the n-1 n vertices or edges.

Since u continuously added to the collection point, it must be updated simultaneously minimum cost side; the need for an auxiliary array closedge, set to maintain each vertex v in the set minimum cost side information u

 

Specific code:

Kruskal:

// the Kruskal 
#include <bits / STDC ++ H.> The using namespace STD; struct Node {
     int a, B; // start point side is a, endpoint B int len; // side length }; 
Node Edge [ 10005 ] ;
 int Father [ 10005 ];
 int n-, m;   // n-number of vertices is, m is the number of edges int CMP (Node X, Y Node) { // press a side ascending sort return x.len < Y. len; 
} int getfather ( int X) { // determines whether two vertices belong to the spanning tree if
 


    




    


    (! X = Father [X]) Father [X] = getfather (Father [X]);
     return Father [X]; 
} 

void Kruskal () {
     int X, Y;
     int K = 0 , CNT = 0 , TOT = 0 ;   // CNT frequency statistics were combined to give the minimum spanning tree is n 1-times 
    the while (CNT <N- . 1 ) { 
        K ++ ; 
        X = getfather (Edge [K] II.A); 
        Y = getfather (Edge [ K] .B);
         IF ! (X = Y) { 
            Father [X] = Y; 
            TOT + = edge[k].len;
            cnt++;
        }
    }
    cout << tot;
}

int main() {
    cin >> n; //农场个数
    int temp = 0;
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=n; j++) {
            int t;
            cin >> t;
            if(i != j) {
                temp++;
                edge[temp].a = i;
                edge[temp].b = j;
                edge[temp].len = t;
            }
        }
    }
    for(int i=1; i<=n; i++) {
        father[i] = i;
    }
    sort(edge+1,edge+1+temp,cmp);
    kruskal();
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/zhhhb/p/12363337.html