Kruskal algorithm has Tai Hang Hdu 1102

Source: Internet
Author: User

Constructing RoadsTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others) Total Submission (s): 15475 Accepted Submission (s): 5907

Problem Descriptionthere is N villages, which is numbered from 1 to N, and we should build some roads such that every t Wo 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 are connected.

We know that there be already some roads between some villages and your job is the build some roads such so all the Vil Lages is connect and the length of 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


Kruskal algorithm has Tai Hang Hdu 1102

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.