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

POJ 2421 Constructing Roads

时间:2017-02-06 10:26:21      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:std   tin   break   length   blog   mil   ever   build   表示   

Constructing Roads
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 23309   Accepted: 9997

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

PKU Monthly,kicc
题目大意时间:给出n(表示点数),n*n的矩阵,表示两点之间的距离,再给出m对数x,y表示x,y本来就连通,求最小生成树
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 using namespace std;
 6 int n,m,x,tot,y,fa[1005];
 7 #define maxm 10010
 8 struct node{
 9     int from,to,value; 
10     bool operator < (const node& a) const {
11         return value<a.value;
12     }
13 }edge[maxm*4];
14 void add_edge(int u,int v,int w){
15     edge[++tot].from=u;edge[tot].to=v;edge[tot].value=w;
16 }
17 int find(int x){
18     if(x==fa[x]) return x;
19     else return fa[x]=find(fa[x]);
20 }
21 int main()
22 {
23     scanf("%d",&n);
24     for(int i=1;i<=n;i++)
25         for(int j=1;j<=n;j++){
26             scanf("%d",&x);
27              add_edge(i,j,x);
28         }
29     scanf("%d",&m);
30     for(int i=1;i<=m;i++){
31         scanf("%d%d",&x,&y);
32         add_edge(x,y,0);add_edge(y,x,0);
33     }
34     sort(edge+1,edge+tot+1);
35     int t=0,ans=0;
36     for(int i=1;i<=n+5;i++) fa[i]=i;
37     for(int k=1;k<=tot;k++){
38         int x=edge[k].from,y=edge[k].to;
39         int rx=find(x),ry=find(y);
40         if(rx!=ry){
41             ans+=edge[k].value;
42             fa[rx]=ry;
43             t++;
44         }
45         if(t== n-1 ) break;
46     }
47     printf("%d\n",ans);
48     return 0;
49 } 

思路:还用说嘛!!啊!说一下吧,前面的数据建立边表,后面的数据将从x到y、y到x的距离改成0,跑一边Kruskal

POJ 2421 Constructing Roads

标签:std   tin   break   length   blog   mil   ever   build   表示   

原文地址:http://www.cnblogs.com/suishiguang/p/6347043.html

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