(Heu step 6.1.1) Constructing Roads (Minimum Spanning Tree template question: Minimum Cost for connecting n points), heuconstructing

Source: Internet
Author: User

(Heu step 6.1.1) Constructing Roads (Minimum Spanning Tree template question: Minimum Cost for connecting n points), heuconstructing

Question:

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 are N versions, which are numbered from 1 to N, and you shoshould build some roads such that every two versions ages can connect to each other. we say two 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 is a road between A and C, and C and B are connected.

We know that there are already some roads between some versions ages and your job is the build some roads such that all the versions are connect and the length of all the roads built is minimum.
 
InputThe first line is an integer N (3 <= N <= 100), which is the number of ages. then come N lines, the I-th of which contains N integers, and the j-th of these N integers is the distance (the distance shocould be an integer within [1, 1000]) between village I and village j.

Then there is an integer Q (0 <= Q <= N * (N + 1)/2 ). then come Q lines, each line contains two integers a and B (1 <= a <B <= N), which means the road between village a and village B has been built.
 
OutputYou shoshould output a line contains an integer, which is the length of all the roads to be built such that all the versions are connected, and this value is minimum.
 
Sample Input
30 990 692990 0 179692 179 011 2
 
Sample Output
179
 
 
Sourcekicc
RecommendEddy



Question Analysis:

Kruscal is a simple question for finding the Minimum Spanning Tree. Note the following:

1) Some methods have been fixed: Set the edge weight to 0.

Map [a] [B] = map [B] [a] = 0; // for an existing edge, set its weight to 0.


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

Int cnt = 1;
For (I = 1; I <= n; ++ I) {// converts link information in the form of a matrix to an edge.
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:

/**. Cpp ** Created on: March 9, 2015 * Author: Administrator */# include <iostream> # include <cstdio> # include <algorithm> using namespace std; const int maxn = 101; struct Edge {// Edge int begin; // start point int end; // end point int weight; // Edge weight} edges [maxn * maxn]; int father [maxn]; int map [maxn] [maxn];/*** find the root of the query set containing node a */int find (int) {if (a = father [a]) {return a;} return father [a] = find (father [a]);} /*** use kruscal to calculate 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 are not connected to father [fx] = fy; // they are connected to sum + = edges [I]. weight; // Add the cost of this route to the total cost} return sum;} bool cmp (const Edge & a, const Edge & B) {return. weight <B. weight;} int main () {int n; while (scanf ("% d", & n )! = EOF) {int I; int j; for (I = 1; I <= n; ++ I) {// read the connection information in the form of a matrix for (j = 1; j <= n; ++ j) {scanf ("% d ", & map [I] [j]) ;}} int m; scanf ("% d", & m); while (m --) {int a, B; scanf ("% d", & a, & B); map [a] [B] = map [B] [a] = 0; // for an existing edge, set its weight to 0.} int cnt = 1; for (I = 1; I <= n; ++ I) {// convert the link information in the form of a matrix into an edge form for (j = 1; j <= I; ++ j) {edges [cnt]. begin = I; edges [cnt]. end = j; edges [cnt ++]. weight = map [I] [j] ;}} cnt-= 1; // used to process the case where the number of edges is greater than 1 due to ++. sort (edges + 1, edges + 1 + cnt, cmp); // This reflects the greedy idea in kruscal printf ("% d \ n", kruscal (cnt);} 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.