题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102
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.
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
prim算法代码如下:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int inf=999999;
int n,dis[111],visit[111],e[111][111];
int main()
{
int i,j,k,u,v,w;
int min;
while(scanf("%d",&n)!=EOF)
{
if(n==0)
break;
int s,q,a,b;
//直接输入图就好了。
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&e[i][j]);
}
}
scanf("%d",&q);
while(q--)
{
scanf("%d%d",&a,&b);
e[a][b]=e[b][a]=0;//让已连通的权值为0
}
for(i=1;i<=n;i++)
{
dis[i]=e[1][i];
}
memset(visit,0,sizeof(visit));
visit[1]=1;
int sum=0;
int count=1;
while(count<n)
{
min=inf;
for(i=1;i<=n;i++)
{
if(visit[i]==0&&dis[i]<min)
{
min=dis[i];
j=i;
}
}
visit[j]=1;
count++;
sum=sum+dis[j];
for(k=1;k<=n;k++)
{
if(visit[k]==0&&dis[k]>e[j][k])
dis[k]=e[j][k];
}
}
printf("%d\n",sum);
}
return 0;
}
kruskal算法代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct edge{
int u,v,w;
}e[11111];
int n;
int pre[111111];
int find(int v)
{
if(pre[v]==v)
return v;
else
{
pre[v]=find(pre[v]);
return pre[v];
}
}
int merge(int u,int v)
{
int t1=find(u);
int t2=find(v);
if(t1!=t2)
{
pre[t2]=t1;
return 1;
}
return 0;
}
bool cmp(edge a,edge b)
{
return a.w<b.w;
}
int main()
{
int i,j,s,q,a,b;
while(~scanf("%d",&n))
{
if(n==0)
break;
//初始化
for(i=1;i<=n;i++)
{
pre[i]=i;
}
int m=1;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&s);
if(i>=j)
continue;
e[m].u=i;
e[m].v=j;
e[m].w=s;
m++;
}
}
cin>>q;
for(i=1;i<=q;i++)
{
cin>>a>>b;
//已连通的点让他们的根节点相同
int fx=find(a);
int fy=find(b);
if(fx!=fy)
pre[fy]=fx;
}
sort(e+1,e+m,cmp);//此处为e+m
int count=0,sum=0;
for(i=1;i<m;i++)
{
if(merge(e[i].u,e[i].v))
{
count++;
sum=sum+e[i].w;
}
if(count==n-1)
break;
}
printf("%d\n",sum);
}
return 0;
}