Hdu Constructing Roads (Minimum Spanning Tree, kuskal algorithm)

Source: Internet
Author: User

Hdu Constructing Roads (Minimum Spanning Tree, kuskal algorithm)
Constructing RoadsTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission (s): 14569 Accepted Submission (s): 5530

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
Question: Give the total number of villages, and then list all the weights from the I node to the j node in the n column below, for the last number entered, enter q to indicate that there are q groups of data, that the next q group data is connected, and find the minimum weight of the final minimal spanning tree! TIPS: Read questions accidentally and read the wrong questions. I wasted two hours doing this! The Code is as follows:
# Include
 
  
# Define deusing namespace std; struct node {int a, B, c;} s [100100]; int father [110]; int cmp (node x, node y) // calculate the minimum weight of the Spanning Tree in ascending order. {Return x. c
  
   
= J) // The bottom Triangle Matrix of the sparse matrix {continue;} s [m]. a = I; s [m]. B = j; s [m]. c = d; m ++; // count coordinate points in the lower Triangle Matrix} scanf ("% d", & q); for (I = 0; I
   
    

Related Article

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.