Constructing Roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17142 Accepted Submission(s): 6507
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
题意:第一次给出一个数n,表示有n个村庄,接下来有n行,每行有n列,分别表示第i个村庄到第j个村庄的距离,比如第一行:0 表示1号村庄与1号村庄的距离,990 表示1号村庄与2号村庄的距离,692表示1号村庄与3号村庄的距离,第二行:990 表示2号村庄与1号村庄的距离,0 表示2号村庄与2号村庄的距离,179 表示2号村庄与3号村庄的距离。。。然后有输入一个数q,接下来是q行,每行表示这两个村庄已经联通。
思路:由所给样例可知,对角线为0,所以只取对角线上方或下方的数据即可。
代码:
Kruskal算法:
#include<stdio.h>
#include<algorithm>
using namespace std;
struct record
{
int s,e,w; //记录边的起点和终点;
}num[10000];
bool cmp(record a,record b)
{
return a.w<b.w; //按距离从小到大排序;
}
int per[110];
int n;
int init()
{
for(int i=1;i<=n;i++)
{
per[i]=i;
}
}
int find(int x)
{
int r;
r=x;
while(r!=per[r])
{
r=per[r];
}
per[x]=r;
return r;
}
int join(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
per[fx]=fy;
return true;
}
return false;
}
int main()
{
int i,j,k,sum,a,b,c,q;
while(scanf("%d",&n)!=EOF)
{
k=0;sum=0;
init();
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&c);
if(j>i) //取对角线的上方的数据;
{
num[k].s=i;
num[k].e=j;
num[k].w=c;
k++;
}
}
}
scanf("%d",&q);
for(i=0;i<q;i++)
{
scanf("%d%d",&a,&b);
join(a,b); //将已联通的村庄连接;
}
sort(num,num+k,cmp);
for(i=0;i<k;i++)
{
if(join(num[i].s,num[i].e))
{
sum+=num[i].w;
}
}
printf("%d\n",sum);
}
return 0;
}
Prim算法:
#include<stdio.h>
#include<string.h>
#define INF 0x3f3f3f
int map[110][110],vis[110],low[110];
int n;
int prim()
{
int next,i,j,sum=0,min;
memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++)
{
low[i]=map[1][i];
}
vis[1]=1;
for(i=2;i<=n;i++)
{
min=INF;
for(j=1;j<=n;j++)
{
if(!vis[j]&&low[j]<min)
{
min=low[j];
next=j;
}
}
sum+=min;
vis[next]=1;
for(j=1;j<=n;j++)
{
if(!vis[j]&&low[j]>map[next][j])
{
low[j]=map[next][j];
}
}
}
return sum;
}
int main()
{
int i,j,a,m,b,c;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&a);
map[i][j]=map[j][i]=a;
}
}
scanf("%d",&m);
for(i=1;i<=m;i++)
{
scanf("%d%d",&b,&c);
map[b][c]=map[c][b]=0;
}
printf("%d\n",prim());
}
return 0;
}