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;
}

  

 

Agri-Net(最小生成树)的更多相关文章

  1. 最小生成树(Kruskal算法-边集数组)

    以此图为例: package com.datastruct; import java.util.Scanner; public class TestKruskal { private static c ...

  2. 最小生成树计数 bzoj 1016

    最小生成树计数 (1s 128M) award [问题描述] 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一 ...

  3. poj 1251 Jungle Roads (最小生成树)

    poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...

  4. 【BZOJ 1016】【JSOI 2008】最小生成树计数

    http://www.lydsy.com/JudgeOnline/problem.php?id=1016 统计每一个边权在最小生成树中使用的次数,这个次数在任何一个最小生成树中都是固定的(归纳证明). ...

  5. 最小生成树---Prim算法和Kruskal算法

    Prim算法 1.概览 普里姆算法(Prim算法),图论中的一种算法,可在加权连通图里搜索最小生成树.意即由此算法搜索到的边子集所构成的树中,不但包括了连通图里的所有顶点(英语:Vertex (gra ...

  6. Delaunay剖分与平面欧几里得距离最小生成树

    这个东西代码我是对着Trinkle的写的,所以就不放代码了.. Delaunay剖分的定义: 一个三角剖分是Delaunay的当且仅当其中的每个三角形的外接圆内部(不包括边界)都没有点. 它的存在性是 ...

  7. 最小生成树(prim&kruskal)

    最近都是图,为了防止几次记不住,先把自己理解的写下来,有问题继续改.先把算法过程记下来: prime算法:                  原始的加权连通图——————D被选作起点,选与之相连的权值 ...

  8. 最小生成树 prime poj1258

    题意:给你一个矩阵M[i][j]表示i到j的距离 求最小生成树 思路:裸最小生成树 prime就可以了 最小生成树专题 AC代码: #include "iostream" #inc ...

  9. 最小生成树 prime + 队列优化

    存图方式 最小生成树prime+队列优化 优化后时间复杂度是O(m*lgm) m为边数 优化后简直神速,应该说对于绝大多数的题目来说都够用了 具体有多快呢 请参照这篇博客:堆排序 Heapsort / ...

  10. 最小生成树 prime poj1287

    poj1287 裸最小生成树 代码 #include "map" #include "queue" #include "math.h" #i ...

随机推荐

  1. free -g 说明

    free -g 说明: free -g -/+ buffers/cache 说明: buffer 写缓存,表示脏数据写入磁盘之前缓存一段时间,可以释放.sync命令可以把buffer强制写入硬盘 ca ...

  2. 关于Django中JsonResponse返回中文字典编码错误的解决方案

    解决方案:JsonResponse(data, json_dumps_params={'ensure_ascii':False}) ! data是需要渲染的字典 def master(request) ...

  3. keepalived+nginx+tomcat+redis实现负载均衡和session共享(原创)

    keepalived+nginx+tomcat+redis实现负载均衡和session共享 直接上链接,码了一天,就不再重写了,希望能帮到大家,有问题欢迎留言交流.

  4. sql查询关于时间的一些汇总

    今天的所有数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())=0 昨天的所有数据:select * from 表名 where ...

  5. iOS 清理Xcode项目中没有使用到的图片资源和类文件

    接手到一个旧的项目,但是发现里面有太多的无用资源,包括升级app后,一些无用的图片资源并没有被删掉,导致app在打包成ipa包以后,文件变大.手边这个项目IM要更换成环信的IM,之前的一些旧的SDK, ...

  6. STM32_3 时钟初始化分析

    在startup文件中,调用了2个函数,一个是System_Init, 另一个是main. System_Init()在system_stm32f10x.c 这个文件中,先看一下时钟树,再分析一下这个 ...

  7. MongoDB入门---文档查询操作之条件查询&and查询&or查询

    经过前几天的学习之路,今天终于到了重头戏了.那就是文档查询操作.话不多说哈,直接看下语法: db.collection.find(query, projection) query :可选,使用查询操作 ...

  8. C++三元操作符

    c++的三元操作符形式: //条件表达式 ? 表达式1 : 表达式2; 语义:如果“条件表达式”为true,则整个表达式的值就是表达式1,忽略表达式2:如果“条件表达式”为false,则整个表达式的值 ...

  9. Ubentu下命令行安装chrome浏览器

    前言: 最近在使用Ubuntu 系统.编译Android aosp 项目.准备写博客,但是Ubuntu 的默认浏览器 firefox 在写csdn 的时候,加载不出来.如下图 一直卡在这里. 这种情况 ...

  10. 利尔达NB-IOT模组Coap数据AT+NMGS发送时返回-513的原因

    1. 利尔达NB-IOT模组使用AT+NMGS发送数据,返回-513的问题,大致有3种可能性,在硬件上,模组的射频电路分为A型和B型模组,所以烧写固件的时候,也要分为A和B型固件,如果烧写反了,那么R ...