Hdoj 1102 Constructing Roads

Source: Internet
Author: User

and check set + minimum spanning tree

Constructing Roads

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 15844 Accepted Submission (s): 6028


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 Input30 990 692990 0 179692 179 011 2

Sample Output179 title means   first enter a number n   represents the number of cities (both test data 3) The next n rows, the number of n per row, set i=n;j=n the meaning of each row is the distance between the first city and the J City (for example, line 0  990 692 meaning for the first city between the distance of the first city for 0, the first city with the second city distance between 990, the first city and the third city distance for 692) Attach the AC code Kruskal algorithm
#include <stdio.h> #include <algorithm>int set[110];using namespace std;struct record{int a;int b;int ju;} S[200000];int find (int fa) {int ch=fa;int t;while (Fa!=set[fa]) fa=set[fa];while (CH!=FA) {t=set[ch];set[ch]=fa;ch=t;} return FA;} void Mix (int x,int y) {int fx,fy;fx=find (x); Fy=find (y); if (Fx!=fy) set[fx]=fy;} BOOL CMP (record A,record b) {return a.ju<b.ju;}    int main () {int n,m,j,i,sum,ju1,village,k,q,v1,v2;while (scanf ("%d", &village)!=eof) {k=0;m=0;    for (i=0;i<=village;i++) {set[i]=i; } for (i=1;i<=village;i++)//Note here should start from 1 because there is no No. 0 City {for (j=1;j<=village;j++) {scanf ("%d", &AMP;JU1)    ;        if (j>i)//This cycle is credited to the distance between all two city combinations {//and Yingcheng number s[k].a=i;    S[k].b=j;    S[K].JU=JU1;    k++;    }}} sort (s,s+k,cmp);    scanf ("%d", &q);    for (i=0;i<q;i++) {scanf ("%d%d", &v1,&v2);    Mix (V1,V2);    } sum=0; for (i=0;i<k;i++) {if (Find (S[I].A)!=find (s[i].b)) {Mix (s[i].a,s[i].b);    Sum+=s[i].ju; }} printf ("%d\n", sum);} return 0;}

  

Hdoj 1102 Constructing Roads

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.