Constructing Roads--hdu1102

Constructing Roads

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


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
 
 
 
 
 
看了好久才看懂,前三行的意思是,第1,2,3个村子分别距离第1,2,3个村子的距离,
 
 
最小生成树来写!
 
 
 
 
 
 
 
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 int per[110],map[101][101];
 6 struct node
 7 {
 8     int b,e,w;
 9 }s[10000];
10 bool cmp(node x,node y)
11 {
12     return x.w<y.w;
13 }
14 void init()
15 {
16     for(int i=1;i<110;i++)
17     per[i]=i;
18  } 
19  
20  int find(int x)
21  {
22      while(x!=per[x])
23      x=per[x];
24      return x;
25  }
26  
27  bool join (int x,int y)
28  {
29      int fx=find(x);
30      int fy=find(y);
31      if(fx!=fy)
32      {
33          per[fx]=fy;
34          return true;
35      }
36      return false;
37  }
38  int main()
39  {
40      int n,i,n1,a,b,j;
41      while(scanf("%d",&n)!=EOF)
42      {
43          init();
44          for(i=1;i<=n;i++)
45             for(j=1;j<=n;j++)
46                  scanf("%d",&map[i][j]);
47          scanf("%d",&n1);     
48         for(i=0;i<n1;i++)
49         {
50             scanf("%d%d",&a,&b);
51             map[a][b]=0;
52         }
53              int k=0;
54              for(i=1;i<=n;i++)
55              {
56                  for(j=i;j<=n;j++)
57                  {
58                      s[k].b=i;
59                      s[k].e=j;
60                      s[k].w=map[i][j];
61                      k++;
62                  }
63              }
64         sort(s,s+k,cmp);
65         int sum=0;
66         for(i=0;i<k;i++)
67         {
68             if(join(s[i].b,s[i].e))
69             sum+=s[i].w;
70         }
71         printf("%d
",sum);
72       }     
73      return 0;
74  }
75  
原文地址:https://www.cnblogs.com/Eric-keke/p/4722157.html