HDU1102--Constructing Roads(最小生成树)

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

kicc


Recommend

Eddy

最小生成树的模板题目

下面的代码使用了Prim算法

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<map>
 7 #include<iomanip>
 8 #include<queue>
 9 #define INF 0x7ffffff
10 #define MAXN 200
11 using namespace std;
12 const double eps=1e-10;
13 const double PI=acos(-1);
14 int G[MAXN][MAXN];
15 int vnew[MAXN];
16 int lowval[MAXN];
17 int sum;
18 int n,q;
19 void Prim(int start)
20 {
21     int j,mi;
22     for(int i=1;i<=n;i++){
23         if(i!=start){
24             lowval[i]=G[start][i];
25             vnew[i]=0;
26         }
27     }
28     vnew[start]=1;
29     for(int i=1;i<=n-1;i++){
30         j=-1;
31         mi=INF;
32         for(int i=1;i<=n;i++){
33             if(vnew[i]==0&&lowval[i]<mi){
34                 j=i;
35                 mi=lowval[i];
36             }
37         }
38             vnew[j]=1;
39             sum+=lowval[j];
40             for(int i=1;i<=n;i++){
41                 if(vnew[i]==0){
42                     lowval[i]=min(lowval[i],G[j][i]);
43                 }
44             }
45     }
46 }
47 int main()
48 {
49     #ifndef ONLINE_JUDGE
50     freopen("data.in", "r", stdin);
51     #endif
52     std::ios::sync_with_stdio(false);
53     std::cin.tie(0);
54     //Prim算法
55     int a,b;
56     while(cin>>n){
57         for(int i=1;i<=n;i++){
58             for(int j=1;j<=n;j++){
59                 cin>>G[i][j];
60             }
61         }
62         cin>>q;
63         for(int i=0;i<q;i++){
64             cin>>a>>b;
65             G[a][b]=G[b][a]=0;
66         }
67         sum=0;
68         Prim(1);
69         cout<<sum<<endl;
70     }
71
72 }
时间: 2024-10-17 10:29:10

HDU1102--Constructing Roads(最小生成树)的相关文章

POJ2421 &amp; HDU1102 Constructing Roads(最小生成树)

嘎唔!~又一次POJ过了HDU错了...不禁让我想起前两天的的Is it a tree?   orz..这次竟然错在HDU一定要是多组数据输入输出!(无力吐槽TT)..题目很简单,炒鸡水! 题意: 告诉你每个村庄之间的距离,和几组已经联通的村庄,求使所有村庄联通所要建的道路的最短距离. 很简单,用最小生成树的Prim算法,相当于邻接矩阵已知,把已联通的村庄之间的距离改为零即可. 附上AC代码: 1 #include <stdio.h> 2 #include <string.h> 3

HDU1102 Constructing Roads 【最小生成树Prim】

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13756    Accepted Submission(s): 5223 Problem Description There are N villages, which are numbered from 1 to N, and you should

HDU 1102 Constructing Roads (最小生成树)

最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1表示原图不连通 */ const int INF = 0x3f3f3f3f; const int MAXN = 110; bool vis[MAXN]; int lowc[MAXN]; int map[MAXN][MAXN]; int Prim(int cost[][MAXN], int n) {

(heu step 6.1.1)Constructing Roads(最小生成树模板题:求让n个点连通的最小费用)

题目: Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 207 Accepted Submission(s): 135   Problem Description There are N villages, which are numbered from 1 to N, and you should bu

HDU1102 Constructing Roads【Prim】

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14897    Accepted Submission(s): 5677 Problem Description There are N villages, which are numbered from 1 to N, and you should

POJ 2421 Constructing Roads (最小生成树)

Constructing Roads Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2421 Appoint description:  System Crawler  (2015-05-27) Description There are N villages, which are numbered from 1 to N, and y

hdu 1102 Constructing Roads 最小生成树Prim算法AC 水~~~

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15230    Accepted Submission(s): 5826 Problem Description There are N villages, which are numbered from 1 to N, and you should

hdu1102 Constructing Roads (简单最小生成树Prim算法)

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

hdu1102 Constructing Roads 基础最小生成树

1 //克鲁斯卡尔(最小生成树) 2 #include<cstdio> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 7 const int maxn = 100005; 8 int n, t; 9 struct node 10 { 11 int bagin, end, len; 12 }arr[maxn]; 13 int fa[maxn]; 14 15 void init() 1

POJ - 2421 Constructing Roads (最小生成树)

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