一、Description(poj1258)

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.

二、问题分析

        典型的求最小生成树问题,算法大多用Prim或者Kruskal算法。小弟用了Prim算法,这道题如果掌握了Prim算法,那么问题就迎刃而解了。可惜小弟数据结构学得不怎么踏实,有临时学了一遍,有点蛋疼。

从单一顶点开始,普里姆算法按照以下步骤逐步扩大树中所含顶点的数目,直到遍及连通图的所有顶点。

  1. 输入:一个加权连通图,其中顶点集合为V,边集合为E;
  2. 初始化:Vnew = {x},其中x为集合V中的任一节点(起始点),Enew = {};
  3. 重复下列操作,直到Vnew = V:
    1. 在集合E中选取权值最小的边(u, v),其中u为集合Vnew中的元素,而v则不是(如果存在有多条满足前述条件即具有相同权值的边,则可任意选取其中之一);
    2. 将v加入集合Vnew中,将(u, v)加入集合Enew中;
  4. 输出:使用集合Vnew和Enew来描述所得到的最小生成树。

这里我们改一下算法,是输出的是权值和。

三、Java代码

import java.util.Scanner;

public class Test {
public static int prim(int[][] a, int count) {
int sum = 0;
int i, j, k;
int[] lowcost = new int[count]; //保存相关定点间的权值
int[] adjvex = new int[count]; //保存相关定点的下标
for (i = 0; i < count; i++) {
lowcost[i] = a[0][i];
adjvex[i]=0;
}
for (i = 1; i < count; i++) {
j=0;
while(lowcost[j]==0){
j++;
}
for (k = 1; k < count; k++) {
if ((lowcost[k]!=0) && (lowcost[k] < lowcost[j])) {
j = k;
}
}
sum += lowcost[j];
lowcost[j] = 0;
for (k = 1; k < count; k++) {
if (lowcost[k]!=0 && (a[j][k] < lowcost[k])) {
{
lowcost[k] = a[j][k];
adjvex[k] = j;
}
}
}
}
return sum;
}
public static void main(String[] args) { Scanner cin=new Scanner(System.in);
int n;
while((n=cin.nextInt())!=-1){
int vertexNumber = n;
int arr[][] = new int[vertexNumber][vertexNumber];
for (int i = 0; i < vertexNumber; i++) {
for (int j = 0; j < vertexNumber; j++) {
arr[i][j]=cin.nextInt();
}
}
System.out.println(prim(arr,n));
n=-1;
}
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Poj1258_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. 【python】-- 列表

    Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推 列表 1.定义列表,取出列表中的值 names = [] #定义空列表 ...

  2. BZOJ3211花神游历各国

    BZOJ3211花神游历各国 BZOJ luogu 分块 记一个all表示该块是否全部<=1,如果all不为真就暴力修改 因为一个数被开根的次数不多,即使\(10^{12}\)只要开根6次也会变 ...

  3. 教你管理SQL备份与恢复系列(1-20)

    原链接:https://bbs.51cto.com/thread-1147908-1.html 教你备份与恢复数据库,直接下面下文档吧. 教你备份与恢复数据库(1)事务 http://bbs.51ct ...

  4. java面向对象入门之带参方法创建

    /* Name :创建带参的方法 Power by :Stuart Date:2015.4.25 */ //创建Way类 class Way{ //Way类成员的基本变量 int add1=123; ...

  5. Spark0.9.0机器学习包MLlib-Classification代码阅读

    本章主要讲述MLlib包里面的分类算法实现,目前实现的有LogisticRegression.SVM.NaiveBayes ,前两种算法针对各自的目标优化函数跟正则项,调用了Optimization模 ...

  6. 【C语言】Linux C调用系统命令

    最近研究深度学习,做视频分析和检测,用到C语言,以前都是写python的,不过没关系,计算机语言都是相通的,差不多原理是一样的,只是语法不太一样. 下面介绍linux C语言种调用本地命令,访问一个地 ...

  7. LeetCode:零钱兑换【322】【DP】

    LeetCode:零钱兑换[322][DP] 题目描述 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成 ...

  8. ButterKnife 原理解析

    一.使用方法 1.添加依赖. implementation 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewhart ...

  9. Jmeter使用文档(windows)

    1. 安装jdk并配置环境变量 以1.8为例: (1)安装jdk1.8; (2)在系统变量里点击新建,变量名填写JAVA_HOME,变量值填写JDK的安装路径“C:\Program Files\Jav ...

  10. CSS3自定义Checkbox特效

    在线演示 本地下载