问题描述
总时间限制: 1000ms 内存限制: 65536kB
描述
Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.
输入
The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.
输出
For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.
样例输入
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
样例输出
28
问题解决
最小生成树问题,个人感觉还是Kruscal算法比较简单。
Prim:
prim算法是一个加点法,共循环N-1次,每次找出离已生成的树最近的点,加入到树里,然后再更新看以这个新的点为边界,每个点有没有离这个树更近的边。
该解法与Dijkstra算法类似,不同的是,Dijkstra算法每次更新的是点到源点的最小距离,是多条边的和,而Prim算法更新的是到该树的最小距离,是一条边的距离。
#include <iostream>
#include <algorithm>
#include <queue>
#include <limits.h>
#include <string.h>
using namespace std;
#define MAXN 101
#define INF 0x7fffffff
int N;
int map[MAXN][MAXN];
int dist[MAXN];
bool vis[MAXN];
int Prim(){
int i,j,k;
int tmp=INF,res=0;
memset(vis,false,sizeof(vis));
for(i=0;i<N;i++) dist[i]=map[0][i];
vis[0]=true;
for(i=1;i<N;i++){
for(tmp=INF,j=0;j<N;j++){
if (dist[j]<tmp && vis[j]==false) {
tmp=dist[j];
k=j;
}
}
res+=tmp;
vis[k]=true;
for(j=0;j<N;j++){
if(vis[j]==false && dist[j]>map[k][j]){
dist[j]=map[k][j];
}
}
}
return res;
}
int main(){
while(cin>>N){
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
cin>>map[i][j];
}
}
cout<<Prim()<<endl;
}
return 0;
}
Kruskal:
Kruskal是一个加边法,将所有的边排序,每次找一个最小的,查看是否能加入。如果该边连接的两点属于一个集合,则这条边对于整体的连接没有贡献,将其舍弃,否则就加入。
#include <iostream>
#include <algorithm>
using namespace std;
#define MAXN 101
int map[MAXN][MAXN];
struct edge{
int s,t;
int w;
}edges[10000];
int parent[101];
bool compare(edge x,edge y){
return x.w<y.w;
}
int findset(int x){
if(parent[x]==x) return x;
else return parent[x]=findset(parent[x]);
}
void unionset(int x,int y){
int rootx=findset(x);
int rooty=findset(y);
if(rootx!=rooty){
parent[rootx]=rooty;
}
}
int main(){
int N;
while(cin>>N){
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
cin>>map[i][j];
}
}
int cnt=0,res=0;
for(int i=0;i<N;i++){
for(int j=i+1;j<N;j++){
edges[cnt].s=i;
edges[cnt].t=j;
edges[cnt++].w=map[i][j];
}
}
for(int i=0;i<N;i++){
parent[i]=i;
}
sort(edges,edges+cnt,compare);
// int num=0;
for(int i=0;i<cnt;i++){
if( findset(edges[i].s) != findset(edges[i].t) ){
res+=edges[i].w;
// num++;
unionset(edges[i].s,edges[i].t);
}
// if(num==N-1) break;
}
cout<<res<<endl;
}
return 0;
}