Constructing Roads
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 19654 | Accepted: 8198 |
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
这道题目和普通的最小生成树的区别是图已经被加了几条边,因此我们可以在原图中,将这些被加边的顶点的边删去或者将其权值改为0,然后再用kruskal算法。
注意:当题目给的图已经是一个联通图时,应该输出0.需要注意一下自己的kruskal算法的写法,以防输出其他答案。
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define Maxn 110
using namespace std;
struct line{
int u,v,w;
line(){}
line(int uu,int vv,int ww):u(uu),v(vv),w(ww){}
bool operator<(const line &a)const{
return w<a.w;
}
}edge[Maxn*Maxn];
int fa[Maxn],adj[Maxn][Maxn];
const int inf=0x3f3f3f3f;
void init(int n){
for(int i=1;i<=n;i++)
fa[i]=i;
}
int findset(int x){
return fa[x]==x?x:(fa[x]=findset(fa[x]));
}
void unionset(int a,int b){
fa[a]=b;
}
int kruskal(int n,int tot){
int cnt=0,ans=0;
for(int i=0;i<tot;i++){
int a=findset(edge[i].u),b=findset(edge[i].v);
if(a!=b){
ans+=edge[i].w;
unionset(a,b);
if(++cnt==n-1) break;
}
}
return ans;
}
int main()
{
int n,k,a,b;
while(~scanf("%d",&n)){
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&adj[i][j]);
for(int i=1;i<=n;i++) adj[i][i]=inf;
scanf("%d",&k);
init(n);
int t=n;
while(k--){
scanf("%d%d",&a,&b);
adj[a][b]=adj[b][a]=inf;
a=findset(a),b=findset(b);
if(a!=b) unionset(a,b),t--;
}
int tot=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(adj[i][j]!=inf)
edge[tot++]=line(i,j,adj[i][j]);
sort(edge,edge+tot);
printf("%d\n",kruskal(t,tot));
}
return 0;
}