Constructing Roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21741 Accepted Submission(s): 8374
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算法:注意mp数组可能需要初始化(memset(mp,0x3F,sizeof(mp)))!
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define N 105
#define INF 0x3F3F3F3F
int n,mp[N][N];
bool vis[N]; //标记点是否访问过
int dis[N]; //保存到此点的最短边,只是一条边
int prim() //prim算法是加点法,分为两步:第一步选择点,第二步更新边
{
for(int i=1;i<=n;i++)
dis[i]=INF,vis[i]=false;
dis[1]=0; //从某一点开始
for(int i=1;i<=n;i++) //迭代n次
{
int mmin=INF,temp;
//temp标记要选取的下一个最近的点
for(int j=1;j<=n;j++) //遍历每一个点,寻找当前结点集“所能扩展到的最短边”
if(!vis[j]&&mmin>dis[j]) //只考虑距离结点集仅一条边距离的点(在上一步已被更新过)
mmin=dis[temp=j];
vis[temp]=true;
for(int j=1;j<=n;j++) //因为结点集新加了一个结点,所以需要更新dis数组
if(!vis[j]&&mp[temp][j]<dis[j])
dis[j]=mp[temp][j];
}
int sum=0;
for(int i=1;i<=n;i++)
sum+=dis[i];
return sum;
}
int main()
{
//freopen("input.txt","r",stdin);
while(scanf("%d",&n)==1)
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&mp[i][j]);
int Q;
scanf("%d",&Q);
int u,v;
while(Q--)
{
scanf("%d%d",&u,&v);
mp[u][v]=mp[v][u]=0; //将已经存在的边的权设为0,很关键的一步
}
printf("%d\n",prim());
}
return 0;
}
Kruskal算法:用G++交一直WA,C++就能过,莫名其妙
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 105 //最多点的数目
#define M 5500 //最多边的数目
#define INF 0x3F3F3F3F
int n;
int roadnum;
int mp[N][N]; //图的邻接矩阵
//边的结构,便于对边进行排序
typedef struct
{
int start;
int end;
int value;
}Edge;
Edge road[M];
bool cmp(Edge a,Edge b)
{
return a.value<b.value;
}
int pre[N]; //并查集所使用的数组
int find(int x)
{
if(pre[x]==x) return x;
else return pre[x]=find(pre[x]);
}
bool Merge(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx==fy) return false;
if(fx<fy) pre[fy]=fx;
if(fy<fx) pre[fx]=fy;
return true;
}
int Kruskal() //Kruskal使用加边法,使用并查集数据结构
{
int num=0; //记录当前加入结点集的结点数目
int cost=0;
for(int i=0;i<roadnum;i++) //从小到大遍历每一条边
{
if(Merge(road[i].start,road[i].end))
{
num++;
cost+=road[i].value;
}
if(num==n-1) break;
}
return cost;
}
int main()
{
//freopen("input.txt","r",stdin);
while(scanf("%d",&n)==1)
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&mp[i][j]);
int Q;
scanf("%d",&Q);
int u,v;
while(Q--)
{
scanf("%d%d",&u,&v);
mp[u][v]=mp[v][u]=0;
}
roadnum=0;
for(int i=1;i<=n;i++) //读取所有的道路信息
for(int j=1;j<i;j++)
{
road[roadnum].start=i;
road[roadnum].end=j;
road[roadnum].value=mp[i][j];
roadnum++;
}
sort(road,road+roadnum,cmp);
for(int i=1;i<=n;i++) pre[i]=i;
printf("%d\n",Kruskal());
}
return 0;
}