【最小生成树基础题】Constructing Roads

Constructing Roads

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16884    Accepted Submission(s): 6413


Problem Description
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages 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 villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
 

 

Input
The first line is an integer N (3 <= N <= 100), 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 is the distance (the distance should 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.
 

 

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

 

Sample Input
3 0 990 692 990 0 179 692 179 0 1 1 2
 

 

Sample Output
179
 

代码:2015.7.30

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <queue>
 4 using namespace std;
 5 #define MAX 10000100
 6 int ID[MAX];
 7 int Sum;
 8 struct Node
 9 {
10     int a,b,v;
11     friend bool operator <(Node a,Node b)
12     {
13         return a.v>b.v;
14     }
15 };
16 void Cread(int N)
17 {
18     for(int i=0;i<=N;i++){ID[i]=i;}
19 }
20 int Find(int x)
21 {
22     int TMD=x,TMP;
23     while(TMD!=ID[TMD])TMD=ID[TMD];
24     while(x!=TMD){TMP=ID[x];ID[x]=TMD;x=TMP;}
25     return x;
26 }
27 void Update(int a,int b)
28 {
29     int A=Find(a);
30     int B=Find(b);
31     if(A!=B)ID[A]=B;
32 }
33 int main()
34 {
35     int N,i,j,a,b,A,B,Q;
36     Node NUM;
37     while(scanf("%d",&N)!=EOF)
38     {
39         priority_queue<Node>ID_N;
40         for(i=1;i<=N;i++)
41         {
42             for(j=1;j<=N;j++)
43             {
44                 NUM.a=i;NUM.b=j;
45                 scanf("%d",&NUM.v);
46                 if(i==j)NUM.v=MAX;
47                 ID_N.push(NUM);
48             }
49         }
50         Cread(N);Sum=0;
51         scanf("%d",&Q);
52         for(j=1;j<=Q;j++)
53         {
54             scanf("%d %d",&a,&b);
55             Update(a,b);
56         }
57         while(!ID_N.empty())
58         {
59             NUM=ID_N.top();ID_N.pop();
60             a=NUM.a;b=NUM.b;
61             A=Find(a);B=Find(b);
62             if(A!=B)
63             {
64                 ID[A]=B;
65                 Sum+=NUM.v;
66             }
67         }
68         printf("%d\n",Sum);
69     }
70     return 0;
71 }
View Code

 

 

 
posted @ 2015-07-30 12:59  Wurq  阅读(200)  评论(0编辑  收藏  举报