Kruskal Algorithm for Minimum Spanning Tree of poj 1751 Highways

Source: Internet
Author: User

It is a country with n cities. It is known that some cities have road connections. Ask which roads are added so that all cities can connect to each other at the lowest cost, the cost is the Cartesian distance between two city coordinates;

Is a pure question for finding the minimal spanning tree;

 


First, all edges are sorted by weight from small to large, and then the smallest edge is obtained in sequence. If the two nodes of China Unicom are on two Unicom components, add the edge; otherwise, delete the edge;

The key of the kruskal algorithm is to judge whether two nodes are in a single connected component, so there is enough flag array []; At the beginning, flag [I] = I; in this way, all nodes are connected by a single component, and then when the existing m edge is obtained,

Set the edge link node to u and v, and then set f [u] = f [v];. Then, the two nodes are connected to one connected component, however, the operation is not as simple as f [u] = f [v], because the UNICOM component of u must be connected with the UNICOM component of v, therefore, the flag value of each Unicom component can point to the same node. For specific operations, see the following code:

 


No order is required for the output edge of this question;

 


The reference code is as follows:


[Cpp]
# Include <iostream>
# Include <cstring>
# Include <algorithm>
# Include <cstdlib>
# Include <cmath>
Using namespace std;
Const int MAX_SIZE = 800;
Struct Node
{
Int u, v;
Double w;
Bool operator <(const Node a) const
{
Return w <a. w;
};
} Edge [MAX_SIZE * MAX_SIZE/2];
Struct point
{
Int x, y;
} A [MAX_SIZE];
Int flag [MAX_SIZE], num, n, m;
 
Int getflag (int u)
{
If (flag [u]! = U)
Flag [u] = getflag (flag [u]);
Return flag [u];
}
Void execute ()
{
Int I, j;
For (I = 1; I <= n; I ++)
{
For (j = I + 1; j <= n; j ++)
If (getflag (I )! = Getflag (j ))
{
Edge [num]. u = I;
Edge [num]. v = j;
Edge [num ++]. w = sqrt (a [I]. x-a [j]. x) * (a [I]. x-a [j]. x) + (a [I]. y-a [j]. y) * (a [I]. y-a [j]. y ));
}
}
Sort (edge, edge + num );
For (I = 0; I <num; I ++)
If (getflag (edge [I]. u )! = Getflag (edge [I]. v ))
{
Flag [getflag (edge [I]. u)] = flag [getflag (edge [I]. v)];
Cout <edge [I]. u <''<edge [I]. v <endl;
}
}
 
Int main ()
{
Cin> n;
Int u, v;
Int I;
For (I = 1; I <= n; I ++)
Cin> a [I]. x> a [I]. y;
Cin> m;
For (I = 1; I <= n + 2; I ++)
Flag [I] = I;
Num = 0;
While (m --)
{
Cin> u> v;
Flag [getflag (u)] = flag [getflag (v)];
}
Execute ();
Return 0;
}

# Include <iostream>
# Include <cstring>
# Include <algorithm>
# Include <cstdlib>
# Include <cmath>
Using namespace std;
Const int MAX_SIZE = 800;
Struct Node
{
Int u, v;
Double w;
Bool operator <(const Node a) const
{
Return w <a. w;
};
} Edge [MAX_SIZE * MAX_SIZE/2];
Struct point
{
Int x, y;
} A [MAX_SIZE];
Int flag [MAX_SIZE], num, n, m;

Int getflag (int u)
{
If (flag [u]! = U)
Flag [u] = getflag (flag [u]);
Return flag [u];
}
Void execute ()
{
Int I, j;
For (I = 1; I <= n; I ++)
{
For (j = I + 1; j <= n; j ++)
If (getflag (I )! = Getflag (j ))
{
Edge [num]. u = I;
Edge [num]. v = j;
Edge [num ++]. w = sqrt (a [I]. x-a [j]. x) * (a [I]. x-a [j]. x) + (a [I]. y-a [j]. y) * (a [I]. y-a [j]. y ));
}
}
Sort (edge, edge + num );
For (I = 0; I <num; I ++)
If (getflag (edge [I]. u )! = Getflag (edge [I]. v ))
{
Flag [getflag (edge [I]. u)] = flag [getflag (edge [I]. v)];
Cout <edge [I]. u <''<edge [I]. v <endl;
}
}

Int main ()
{
Cin> n;
Int u, v;
Int I;
For (I = 1; I <= n; I ++)
Cin> a [I]. x> a [I]. y;
Cin> m;
For (I = 1; I <= n + 2; I ++)
Flag [I] = I;
Num = 0;
While (m --)
{
Cin> u> v;
Flag [getflag (u)] = flag [getflag (v)];
}
Execute ();
Return 0;
}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.