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

poj 2421 Constructing Roads

时间:2017-05-08 10:10:02      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:最小   rom   lines   inpu   tab   main   生成树   struct   with   

Constructing Roads

Time Limit: 2000MS   Memory Limit: 65536K
     

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

题目大意:有n个村庄,编号为1~n,我们需要建造道路使它们相互可以到达。一开始有一些道路已经建好了。我们知道每两个村庄间的距离(distance)。求建设道路的最短距离。N<=100

这道题,我们只需要把原本已经连好的边把权值当成0,那么我们在进行最小生成树算法的时候,总会优先把这些边连上。

 1 #include<algorithm>
 2 #include<cstdio>
 3 using namespace std;
 4 #define maxn 105
 5 #define maxm 9905
 6 struct Edge {
 7     int u,v,w;
 8     Edge(int u=0,int v=0,int w=0):
 9         u(u),v(v),w(w) {}
10     bool operator < (const Edge &a) const {
11         return w<a.w;
12     }
13 }edge[maxm];
14 int record[maxn][maxn]={0},father[maxn];
15 int find(int x) {
16     return father[x]==x?x:father[x]=find(father[x]);
17 }
18 int main() {
19     int n,q,cnt=0,mst=0;
20     scanf("%d",&n);
21     for(int i=1;i<=n;i++) {
22         father[i]=i;
23         for(int j=1;j<=n;j++) {
24             scanf("%d",&q);
25             if(q!=0&&j<=i) {
26                 edge[++cnt]=Edge(i,j,q);
27                 record[i][j]=cnt;
28             }
29         }
30     }
31     scanf("%d",&q);
32     while(q--) {
33         int a,b;
34         scanf("%d%d",&a,&b);
35         if(record[a][b]) edge[record[a][b]].w=0;
36         else edge[record[b][a]].w=0;
37     }
38     sort(edge+1,edge+cnt+1);
39     for(int i=1;i<=cnt;i++) {
40         int fu=find(edge[i].u),fv=find(edge[i].v);
41         if(fu!=fv) {
42             father[fu]=fv;
43             mst+=edge[i].w;
44             q++;
45         }
46         if(q+1==n-1)break;
47     }
48     printf("%d\n",mst);
49     return 0;
50 }

 

poj 2421 Constructing Roads

标签:最小   rom   lines   inpu   tab   main   生成树   struct   with   

原文地址:http://www.cnblogs.com/NuclearSubmarines/p/6823359.html

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