题目链接:http://poj.org/problem?id=1258

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

对于最小生成树,一般而言会有两种通用算法:
一:Kruskal算法:对边进行贪心操作(每条边一般而言会有三种属性值:val:边的权值、(x,y):边连接的两个点的编号)
  1)将所有的边按照边的权值进行从小到大的排序(结构体类型的排序,三个属性值是一体的);
  2)设一棵树为T,将边从小到大进行遍历,若是加入这条边不会形成圈(形成圈了,则一定不是树,便一定权值和不是最小),就将这条边加入树T中,如此循环,
    直到遍历完所有的边。最后便会生成一颗最小生成树。(至于如何判断假如一条边会不会形成圈,可以使用并查集的知识进行实现(若新加入的边的两个顶点
    不在一个集合中,则不会形成圈))。
poj1258的Kruskal的AC代码:

#include<iostream> //利用最小生成树中的kruskal算法
#include<cstdio>
#include<algorithm>
using namespace std;
int bin[120];
struct node{
int val,x,y;
}a[10010];
int cmp(struct node a,struct node b){
return a.val<b.val;
}
int find(int x){
if(bin[x]==x) return x;
else return bin[x]=find(bin[x]);
}
void unite(int x,int y){
x=find(x),y=find(y);
bin[x]=y;
}
int main(){
int n,d;
struct node c;
while(cin>>n){
int k=0;
for(int i=1;i<=n;i++) //利用并查集的知识防止生成圈,若生成圈则一定不是最小
bin[i]=i;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
scanf("%d",&d);
if(j>i) a[k].val=d,a[k].x=i,a[k].y=j,k++;
}
sort(a,a+k,cmp); //对边的长度进行排序
int sum=0;
for(int i=0;i<k;i++){//对边从小到大进行遍历
if(find(bin[a[i].x])!=find(bin[a[i].y])) unite(bin[a[i].x],bin[a[i].y]),sum+=a[i].val; //若不会生成圈,则放入集合
}
printf("%d\n",sum);
}
return 0;
}

poj1258Agri-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. 数论(lcm)

    CodeForces - 1154G You are given an array a consisting of n integers a1,a2,…,an . Your problem is to ...

  2. Excel VBA批量处理寸照名字(类模块加FSO版)

    需求:因为处理学生学籍照片,从照相馆拿回来的寸照是按班级整理好,文件名是相机编号的文件.那么处理的话,是这么一个思路,通过Excel表格打印出各班A4照片列表,让学生自行填上照片对应姓名.表格收回来后 ...

  3. 手写一个SpringMVC框架(转)

    一:梳理SpringMVC的设计思路 本文只实现自己的@Controller.@RequestMapping.@RequestParam注解起作用,其余SpringMVC功能读者可以尝试自己实现. 1 ...

  4. Sudoku (剪枝+状态压缩+预处理)

    [题目描述] In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. ...

  5. Java解析XML介绍

    开发十年,就只剩下这套架构体系了! >>>   XML解析器提供了访问或修改用来表示数据的xml文件的能力.Java中提供了多种方式来解析xml文件. 主要分为两类,包括解析XML文 ...

  6. vue框架中实现今天昨天前天最近时间

    点击下拉出几个选项,根据日期不同来过滤数据.看图--(忽略小梨子,这是日常练手页面) (element ui) 点击today-当天日期 点击last three days 点击custom,并且实现 ...

  7. Scrapy抓取jobbole数据

    1.python版本3.6.1 2.python编辑器:JetBrains PyCharm 2.安装virtualenvwrapper-win pip3 install virtualenvwrapp ...

  8. Linux下C语言实现ATM取款机,消息队列版本

    链接:https://pan.baidu.com/s/1oBavXBuZul7ZAEBL1eYfRA 提取码:ffhg Mybank ATM取款机实验,消息队列实现本实验用的是Centos71.在服务 ...

  9. union 横向组合

    select sum(zs) zs,sum(zl) zl,sum(ts) ts,sum(lxcbw) lxcbw,sum(bz) bz,sum(sfzqt) sfzqtfrom (select cou ...

  10. 奇虎360的开源OpenResty Windows版本

    https://github.com/LomoX-Offical/nginx-openresty-windows