Description

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. 

Input

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.

Output

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.

Sample Input

4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0

Sample Output

28

题目意思:农场需要拉互联网来相通,给出了n个农场之间需要连通成本的邻接矩阵,求出所需要的最小成本即最小生成树。

\\\克鲁斯卡尔算法
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,sum;
int k;
struct node
{
int start;///起点
int end;///终点
int power;///权值
}edge[150*150];
int pre[150*150];
int cmp(node a,node b)
{
return a.power<b.power;///按权值排序
}
int find(int x)///并查集找祖先
{
int a;///循环法
a=x;
while(pre[a]!=a)
{
a=pre[a];
}
return a;
}
void merge(int x,int y,int n)
{
int fx =find(x);
int fy =find(y);
if(fx!=fy)
{
pre[fx]=fy;
sum+=edge[n].power;
}
}
int main()
{
int i,j,x;
while(scanf("%d",&n)!=EOF)
{
if(n==0)
{
break;
}
sum=0;
k=1;
for(i=1;i<=n;i++)///并查集的初始化
{
pre[i]=i;
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&x);
edge[k].start=i;
edge[k].end=j;
edge[k].power=x;
k++;
}
}
k--;
sort(edge+1,edge+k+1,cmp);
for(i=1;i<=k;i++)
{
merge(edge[i].start,edge[i].end,i);
}
printf("%d\n",sum);
}
return 0;
}

 

\\\普里姆算法

#include<stdio.h>
#include<string.h>
#define MAX 0x3f3f3f3f
using namespace std;
int logo[150*150];///用0和1来表示是否被选择过
int map1[150][150];
int dis[150*150];///记录任意一点到这一点的最近的距离
int n,m;
int prim()
{
int i,j,now;
int sum=0;
for(i=1;i<=n;i++)///初始化
{
dis[i]=MAX;
logo[i]=0;
}
for(i=1;i<=n;i++)
{
dis[i]=map1[1][i];
}
dis[1]=0;
logo[1]=1;
for(i=1;i<n;i++)///循环查找
{
now=MAX;
int min1=MAX;
for(j=1;j<=n;j++)
{
if(logo[j]==0&&dis[j]<min1)
{
now=j;
min1=dis[j];
}
}
if(now==MAX)///防止不成图
{
break;
}
logo[now]=1;
sum=sum+min1;
for(j=1;j<=n;j++)///填入新点后更新最小距离,到顶点集的距离
{
if(logo[j]==0&&dis[j]>map1[now][j])
{
dis[j]=map1[now][j];
}
}
} printf("%d\n",sum);
}
int main()
{
int i,j,x;
while(scanf("%d",&n)!=EOF)///n是点数
{
if(n==0)
{
break;
}
memset(map1,0x3f3f3f3f,sizeof(map1));///map是邻接矩阵储存图的信息
for(i=1;i<=n;i++)
{ for(j=1;j<=n;j++)
{
scanf("%d",&x);
map1[i][j]=x;
}
}
prim();
}
return 0;
}

  

 

最新文章

  1. poj[2104]K-th Number
  2. [HDU5902]GCD is Funny(xjb搞)
  3. C++读取txt文件
  4. Linux下autoconf和automake使用
  5. iOS学习网站及大牛网址(实时更新)
  6. (DP6.1.2.1)UVA 147 Dollars(子集和问题)
  7. HDU-5347 MZL&#39;s chemistry
  8. 数据库(批处理, 事务,CachedRawSetImpl类
  9. MongoDB集群
  10. 求最短路径算法之SPAF算法。
  11. Flex中对表格中某列的值进行数字格式化并求百分比
  12. java内部类:成员内部类,静态内部类方法内部类,匿名内部类(A)
  13. 在IE中下载Office2007文件时在对话框中下载文件变成ZIP文件的问题
  14. 清空Sql server日志
  15. 游戏AI技术 2
  16. 【贪心】PAT 1033. To Fill or Not to Fill (25)
  17. MVC二级联动使用$.getJSON方法
  18. 《F4+2 团队项目需求分析改进》
  19. CentOS6.5 重启网络报错:Bringing up interface eth0: Error: Connection activation failed: Device not managed by NetworkManager or unavailable
  20. Redis AOF 全持久化

热门文章

  1. 浅谈HashMap与线程安全 (JDK1.8)
  2. mint-ui message box 问题;
  3. day 29 socketserver ftp功能的简单讲解
  4. java集合基础篇 简单总结
  5. Python 爬虫 (四)
  6. keil5最新版安装与破解
  7. VMWare共享文件
  8. C# 使用cmd
  9. go内建容器-Map
  10. pylearn2报错缺少theano.compat.six