Constructing Roads
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 18238 | Accepted: 7529 |
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
PKU Monthly,kicc
解题报告:
此题为最小生成树问题。基本属于模板题。
只需要吧修通了路的地方赋值为0即可,注意是无向连通图。代码如下:
方法一:prim 邻接矩阵
#include<iostream>
#include<cstdio>
#include<cstring>
#define INF 0x7fffffff
using namespace std;
int a[111][111],dist[111],n;
bool vis[111];
int main(){
while(scanf("%d",&n)==1){
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
scanf("%d",&a[i][j]);
if(a[i][j]==0)
a[i][j]=INF;
}
int t,c,d;
scanf("%d",&t);
for(int i=1;i<=t;i++){
scanf("%d%d",&c,&d);
a[c][d]=0;a[d][c]=0;
}
for(int i=1;i<=n;i++)
dist[i]=a[1][i];
vis[1]=true;
int num=1,sum=0;
while(num<n){
int res=INF,pos;
for(int i=1;i<=n;i++)
if(dist[i]<res&&!vis[i]){
pos=i;res=dist[i];
}
vis[pos]=true;
sum+=res;
for(int i=1;i<=n;i++)
if(!vis[i])
dist[i]=min(dist[i],a[pos][i]);
num++;
}
printf("%d\n",sum);
}
return 0;
}
方法二:prim 邻接表
#include<iostream>
#include<cstdio>
#include<cstring>
#define INF 0x7fffffff
using namespace std;
struct edge{
int v,next,cost;
}e[111*111];
int ecnt,first[111];
int n,v;
void addedge(int u,int v,int cost){
e[++ecnt].next=first[u];
e[ecnt].v=v;
e[ecnt].cost=cost;
first[u]=ecnt;
}
int prim(){
bool vis[111];
int dist[111],num=1,sum=0;
memset(vis,0,sizeof(vis));
vis[1]=true;
for(int i=1;i<=n;i++)
dist[i]=INF;
for(int i=first[1];i!=-1;i=e[i].next)
dist[e[i].v]=min(dist[e[i].v],e[i].cost);
while(num<n){
int res=INF,pos;
for(int i=1;i<=n;i++)
if(dist[i]<res&&!vis[i])
pos=i,res=dist[i];
vis[pos]=true;
sum+=res;
for(int i=first[pos]; i!=-1;i=e[i].next){
int v=e[i].v;
if(vis[v]) continue;
dist[v] = min(dist[v], e[i].cost);
}
num++;
}
return sum;
}
int main(){
while(scanf("%d",&n)==1){
memset(first,-1,sizeof(first));
ecnt=-1;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
scanf("%d",&v);
if(v!=0)
addedge(i,j,v);
}
int t,a,b;
scanf("%d",&t);
for(int i=1;i<=t;i++){
scanf("%d%d",&a,&b);
addedge(a,b,0);
addedge(b,a,0);
}
printf("%d\n",prim());
}
return 0;
}
方法三:kruscal
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct edge{
int v,u,cost;
}e[111*111];
bool cmp(const edge &a,const edge &b){
return a.cost<b.cost;
}
int ecnt,fa[111],n,v;
void addedge(int u,int v,int cost){
e[++ecnt].v = v;
e[ecnt].u = u;
e[ecnt].cost = cost;
}
int getf(int x){
return fa[x]==x?x:fa[x]=getf(fa[x]);
}
int kruscal(){
int sum=0;
for(int i=1;i<=n;i++)
fa[i]=i;
sort(e+1,e+1+ecnt,cmp);
for(int i=1;i<=ecnt;i++){
int u=e[i].u,v=e[i].v;
u=getf(u);
v=getf(v);
if(u==v) continue;
sum+=e[i].cost;
fa[u]=v;
}
return sum;
}
int main(){
while(scanf("%d",&n)==1){
ecnt=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
scanf("%d",&v);
if(v!=0)
addedge(i,j,v);
}
int t,a,b;
scanf("%d",&t);
for(int i=1;i<=t;i++){
scanf("%d%d",&a,&b);
addedge(a,b,0);
addedge(b,a,0);
}
printf("%d\n",kruscal());
}
return 0;
}