Poj-2421constructing Roads, is also the smallest spanning tree, and the eighth session of the Henan Provincial Water Diversion project surprisingly similar, and check set and minimum spanning tree flexibility and energy use, water too ~ ~ ~

Source: Internet
Author: User

Constructing Roads
Time Limit: 2000MS Memory Limit: 65536K

Description

There is N villages, which is numbered from 1 to N, and we should build some roads such that every both villages can con Nect to all 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.

Input

The 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.

Output

You should output a line contains an integer, which is the length of the "All" the roads to being built such that all the villages is connected, and this value is minimum.

Sample Input

30 990 692990 0 179692 179 011 2

Sample Output

179

Source

PKU MONTHLY,KICC


Find the smallest spanning tree topic, see this problem but found and Henan province of the "Water Diversion project" amazing similar, anyway that problem did not do it, this plan to first a the problem again do this problem, but did that question and found that the problem is more simple, so the tone code direct A, In fact, I would rather believe that the background test data water;

Let's say the idea : We find that the input is in matrix mode, and if there is no doubt that there is a waste of memory in the structure, we find that many of the same numbers in the matrix, that is, the coordinates of the corresponding points (such as a[i][j]=a[j][i]) are the same value, So we just need to put half the number of matrices in the structure, and then it is the simplest of the smallest spanning tree, as for the Q bar has been connected to the good road we only need to use and check the input data when the root node merging can be; maybe it's a little unclear here, See the code immediately understand;

AC code: To put it bluntly, this problem is the flexible use of the set and the minimum spanning tree ;

#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include < algorithm>using namespace Std;const int n=5100;//data range is 100, the square is 10000, but Q&LT;=10000/2, open 5050 can be over; int n,m,k,f[100]; struct node{int u,v,w;} a[n];int cmp (node A,node b) {return A.W&LT;B.W;} int find (int x) {return f[x]==-1?x:x=find (f[x]);}    int ks (int n) {int ans=0,cot=0;    Sort (a+1,a+1+k,cmp);        for (int i=1; i<=k; i++) {int u=find (A[I].U);        int V=find (A[I].V);            if (u!=v) {ANS+=A[I].W;            F[u]=v;        cot++;    } if (cot==n-1) break; } return ans;    int main () {int x;    scanf ("%d", &n);    k=0;    memset (A,0,sizeof (a));            for (int. I=1; i<=n; i++) for (int j=1; j<=n; J + +) {scanf ("%d", &x);        if (j>i)//half of the data exist in the structure; a[++k].u=i,a[k].v=j,a[k].w=x;    } memset (F,-1,sizeof (f));    scanf ("%d", &m);    int x1,y1; while (m--)   {scanf ("%d%d", &x1,&y1);        int Xx=find (x1);        int Yy=find (y1);            if (Xx!=yy)//root node merge and check set; {f[xx]=yy;        n--;    }} printf ("%d\n", KS (n)); return 0;}


Poj-2421constructing Roads, is also the smallest spanning tree, and the eighth session of the Henan Provincial Water Diversion project surprisingly similar, and check set and minimum spanning tree flexibility and energy use, water too ~ ~ ~

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.