Constructing Roads
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 54 Accepted Submission(s) : 28
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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
Source
kicc
—————
———————————————————————————————
给出n个城市两两之间的花费,再给出几个城市已经建好的边,求让所有城市联通的最小花费
思路:最小生成树
Prim算法:
#include<iostream>
#include<cstdio>
#include<queue>
#include<cmath>
#include<cstring>
using namespace std;
#define inf 0x3f3f3f
int mp[105][105];
int low[105];
int m,n;
void prim(int x)
{
int sum=0;
for(int i=1;i<=n;i++)
{
low[i]=mp[x][i];
}
low[x]=-1;
for(int i=1;i<n;i++)
{
int mn=inf;
int v=-1;
for(int j=1;j<=n;j++)
{
if(low[j]!=-1&&low[j]<mn)
{
mn=low[j];
v=j;
}
}
if(v!=-1)
{
low[v]=-1;
sum+=mn;
for(int j=1;j<=n;j++)
{
if(low[j]!=-1&&mp[v][j]<low[j])
{
low[j]=mp[v][j];
}
}
}
}
printf("%d\n",sum);
}
int main()
{
int u,v,c,k;
while(~scanf("%d",&n)&&n)
{
memset(mp,0,sizeof(mp));
for(int i=1; i<=n; i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&c);
if(i==j)
continue;
else
mp[i][j]=mp[j][i]=c;
}
}
scanf("%d",&m);
for(int i=0;i<m;i++)
{
scanf("%d%d",&u,&v);
mp[u][v]=mp[v][u]=0;
}
prim(1);
}
return 0;
}
kruskal算法:
#include <iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<set>
using namespace std;
#define LL long long
struct node
{
int u,v,w;
} p[1000005];
int n,cnt,pre[1005];
bool cmp(node a,node b)
{
return a.w<b.w;
}
void init()
{
for(int i=0; i<1005; i++)
pre[i]=i;
}
int fin(int x)
{
return pre[x]==x?x:pre[x]=fin(pre[x]);
}
void kruskal()
{
sort(p,p+cnt,cmp);
init();
int cost=0;
int ans=0;
for(int i=0; i<cnt; i++)
{
int a=fin(p[i].u);
int b=fin(p[i].v);
if(a!=b)
{
pre[a]=b;
cost+=p[i].w;
ans++;
}
if(ans==n-1)
{
break;
}
}
printf("%d\n",cost);
}
int main()
{
int mp[105][105],k,x,y;
while(~scanf("%d",&n)&&n)
{
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
scanf("%d",&mp[i][j]);
cnt=0;
for(int i=0; i<n; i++)
for(int j=i+1; j<n; j++)
{
p[cnt].u=i,p[cnt].v=j;
p[cnt++].w=mp[i][j];
}
scanf("%d",&k);
for(int i=0; i<k; i++)
{
scanf("%d%d",&x,&y);
p[cnt].u=x-1,p[cnt].v=y-1;
p[cnt++].w=0;
}
kruskal();
}
return 0;
}