HDU1102--Constructing Roads(最小生成树)
Posted Pic
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.
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 }
以上是关于HDU1102--Constructing Roads(最小生成树)的主要内容,如果未能解决你的问题,请参考以下文章
hdu 1102 Constructing Roads (Prim算法)
HDU1102--Constructing Roads(最小生成树)
hdu1102 Constructing Roads 最小生成树prime