(HEU step 6.1.1) Constructing Roads (minimum spanning tree template title: Ask for the minimum cost of n-point connectivity)

Source: Internet
Author: User

Topic:

Constructing Roads
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 207 Accepted Submission (s): 135
problem Descriptionthere is N villages, which is Numbered from 1 to N, and your should build some roads such that every the villages can connect to each of the other. We say village A and B are connected, if and only if there is a road between A and B, or there exists a village C such That there was a road between A and C, and C and B were connected. 

We know that there be already some roads B Etween some villages and your job is the build some roads such so all the villages be connect and the length of all the Roads built is minimum.
 
Inputthe first line is a integer n (3 <= N <=), which is the number of villages. Then come n lines, the i-th of which contains n integers, and the j-th of these n integers are the distance (the distance s Hould is an integer within [1, +]) between village I and village J.

Then there was an integer q (0 <= q <= n * (n + 1)/2). Then come Q lines, each line contains the integers a and B (1 <= a < b <= N), which means the road between Villag e A and village B has been built.
Outputyou should output a line contains an integer, which is the length of the "All the roads" to be built such Lages is connected, and this value is minimum.
Sample Input
30 990 692990 0 179692 179 011 2
Sample Output
179
Sourcekicc
Recommendeddy



Topic Analysis:

Kruscal minimum spanning tree, simple problem. You need to be aware of the following situations:

1) Some roads have been repaired by the way: the weight of the edge of the bar is set to 0.

MAP[A][B] = Map[b][a] = 0;//for an already existing edge, we set his weight to 0.


2) The connection information is converted from a matrix into an edge form.

int cnt = 1;
for (i = 1; I <= n; ++i) {//Convert the link information to the form of an edge as a matrix
for (j = 1; J <= i; ++j) {
Edges[cnt].begin = i;
Edges[cnt].end = j;
Edges[cnt++].weight = Map[i][j];
}
}


The code is as follows:

/* * a.cpp * * Created on:2015 March 9 * author:administrator * * #include <iostream> #include <cstdio> #incl Ude <algorithm>using namespace Std;const int maxn = 101;struct edge{//edge int begin;//start int end;//endpoint int weight;//Edge Value} Edges[maxn*maxn];int Father[maxn];int map[maxn][maxn];/** * Find the root */int find (int a) {if (a = = Father[a]) {if (a = =) {) {return A;} that contains a node return Father[a] = find (Father[a]);} /** * Use kruscal to find the minimum spanning tree. * Template */int kruscal (int count) {int i;for (i = 1; i < MAXN; ++i) {father[i] = i;} int sum = 0;for (i = 1; I <= count; ++i) {int fx = find (edges[i].begin); int fy = find (edges[i].end); if (FX! = FY) {///if they Not connected father[fx] = fy;//Let them connect the sum + = edges[i].weight;//to add the cost of the road to the total cost of roads}}return sum;} BOOL CMP (const edge& A, const edge& b) {return a.weight < b.weight;}  int main () {int n;while (scanf ("%d", &n)!=eof) {int I;int j;for (i = 1; I <= n; ++i) {//a matrix reads in connection information for (j = 1; J <= n; ++J) {scanf ("%d", &map[i][j]);}} int m;scanf ("%d", &m), while (m--) {int a,b;scanf ("%d%d ", &a,&b); Map[a][b] = map[b][a] = 0;//for an already existing edge, we set his weight to 0 to}int cnt = 1;for (i = 1; I <= n; ++i) {//link information has a matrix The form of conversion into the form of an edge for (j = 1; J <= i; ++j) {edges[cnt].begin = I;edges[cnt].end = J;edges[cnt++].weight = Map[i][j];}} CNT-= 1;//is used to handle more than 1 of the number of edges caused by + + (EDGES+1,EDGES+1+CNT,CMP);//This embodies the greedy mind of kruscal printf ("%d\n", Kruscal (CNT));} return 0;}







(HEU step 6.1.1) Constructing Roads (minimum spanning tree template title: Ask for the minimum cost of n-point connectivity)

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.