码迷,mamicode.com
首页 > 其他好文 > 详细

杭电1102--Constructing Roads(简单并查集)

时间:2015-08-03 16:30:33      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:

Constructing Roads

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


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
 

 

Source
 

 

Recommend
Eddy   |   We have carefully selected several similar problems for you:  1301 1162 1217 3371 1142 
prime:
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5 int i, j, n;
 6 const int INF = 0x3f3f3f3f;
 7 int map[102][102], dis[102], vis[102];
 8 void prime()
 9 {
10     memset(vis, 0, sizeof(vis));   //important; 
11     int sum = 0;
12     for(i=1; i<=n; i++)
13         dis[i] = map[1][i];
14     vis[1] = 1;
15     for(i=1; i<n; i++)
16     {
17         int min = INF, temp;
18         for(j=1; j<=n; j++)
19         {
20             if(!vis[j] && dis[j] < min)
21             {
22                 min = dis[j];
23                 temp = j;
24             }
25         }
26         vis[temp] = 1;
27         sum += min;
28         for(j=1; j<=n; j++)
29         {
30             if(!vis[j] && dis[j] > map[temp][j])
31                 dis[j] = map[temp][j];
32         }
33     }
34     printf("%d\n", sum);
35 }
36 int main()
37 {
38     while(~scanf("%d", &n))
39     {
40         for(i=1; i<=n; i++)
41             for(j=1; j<=n; j++)
42                 cin >> map[i][j];
43         int q;
44         scanf("%d", &q);
45         for(i=1; i<=q; i++)
46         {
47             int a, b;
48             scanf("%d %d", &a, &b);
49             map[a][b] = map[b][a] = 0;        
50         }
51         prime();
52     }
53     return 0;
54 }

//KL;

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <iostream>
 4 using namespace std;
 5 int father[110];
 6 int i, j, n;
 7 struct via
 8 {
 9     int s, e, m;
10 } num[10010];
11 bool cmp(via s, via e)
12 {
13     return s.m < e.m;
14 }
15 int find(int a)
16 {
17     while(a != father[a])
18         a = father[a];
19     return a;  
20 }
21 void mercy(int a, int b)
22 {
23     int q = find(a);
24     int p = find(b);
25     if(q != p)
26         father[q] = p;
27 }
28 int main()
29 {
30     while(~scanf("%d", &n))
31     {
32         int a, b, c, k = 0; 
33         for(i=1; i<= n; i++)
34         {
35             father[i] = i;
36             for(j=1; j<=n; j++)
37             {
38                 scanf("%d", &a);
39                 if(j > i)
40                 {
41                     num[k].s = i;
42                     num[k].e = j;
43                     num[k].m = a;
44                     k++;
45                 }
46             }
47         }
48     //    printf("%d\n", k);
49         sort(num, num+k, cmp);
50         int q;
51         scanf("%d", &q);
52         for(i=1; i<=q; i++)
53         {
54             int u, v;
55             scanf("%d %d", &u, &v);
56             mercy(u, v);
57         }
58         int sum = 0;
59         for(i=0; i<k; i++)
60         {
61             int q = find(num[i].s);
62             int p = find(num[i].e);
63             if(q != p)
64             {
65                 sum += num[i].m;
66                 father[q] = p;
67             }
68         }        
69         printf("%d\n", sum);
70     }
71     return 0;
72 }

 

杭电1102--Constructing Roads(简单并查集)

标签:

原文地址:http://www.cnblogs.com/fengshun/p/4699305.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!