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.
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个村庄,然后是一个map,表示每个村庄的距离,整数Q,接下来Q对村庄,表示这两个村庄已经联通,求通过修路使所有村庄都联通并且总长度最短。
(1) kruskal
#include <iostream>
#include <algorithm>
using namespace std;
int Father[1100];
struct node
{
int a;
int b;
int w;
};
int cmp(node a,node b)
{
return a.w<b.w;
}
int Find(int x)
{
while (x!=Father[x])
{
x=Father[x];
}
return x;
}
void Union(int x,int y)
{
x=Find(x);
y=Find(y);
if(x!=y)
{
Father[x]=y;
}
}
int main()
{
int n;
node num[10000];
int ans,sum;
while (cin>>n && n)
{
ans=0;
sum=0;
for (int i=0; i<=n; i++)
{
Father[i]=i;
}
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
{
int w;
cin>>w;
if(i==j)
{
continue;
}
num[ans].a=i+1;
num[ans].b=j+1;
num[ans].w=w;
ans++;
}
}
int Q;
int k=0;
cin>>Q;
for (int i=0; i<Q; i++)
{
int a,b;
cin>>a>>b;
a=Find(a);
b=Find(b);
if (a!=b)
{
Union(a, b);
k++;
}
}
sort(num, num+ans, cmp);
for (int i=0; i<ans; i++)
{
int x=Find(num[i].a);
int y=Find(num[i].b);
if(x!=y)
{
Union(x, y);
sum+=num[i].w;
k++;
}
if(k>=n-1)
{
break;
}
}
cout<<sum<<endl;
}
return 0;
}
(2)prim
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int map[105][105];
const int Max=1000000;
int n;
int Prim(int v0)
{
int lowcost[105];
int sum=0;
for (int i=0; i<n; i++)
{
lowcost[i]=map[v0][i];
}
lowcost[v0]=-1;
for (int i=0; i<n-1; i++)
{
int min=Max;
int k=-1;
for (int j=0; j<n; j++)
{
if(lowcost[j]!=-1&&min>lowcost[j])
{
min=lowcost[j];
k=j;
}
}
if(k==-1)
{
break;
}
sum+=min;
lowcost[k]=-1;
for (int j=0; j<n; j++)
{
if(lowcost[j]!=-1 && map[k][j]<lowcost[j])
{
lowcost[j]=map[k][j];
}
}
}
return sum;
}
int main()
{
int q;
while (cin>>n)
{
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
{
if(i==j)
{
map[i][j]=0;
}
else
{
map[i][j]=Max;
}
cin>>map[i][j];
}
}
int a,b;
cin>>q;
for (int i=0; i<q; i++)
{
cin>>a>>b;
map[a-1][b-1]=map[b-1][a-1]=0; //置为0则最小肯定能选上
}
cout<<Prim(0)<<endl;
}
return 0;
}