一、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. Android5.0以后版本把应用移动到SD或者TF卡的方法

    由于手机内存较小,才8G,用的时间一久,内部存储就满了,天天删垃圾,WIFI还老断线,终于忍无可忍了,决定把应用移动到SD卡,实践下来,只有少部分App默认支持移动到SD卡,大部分程序不支持只能装在内 ...

  2. 让Xcode支持高版本系统设备真机测试

    最新支持11.2 (15C107) Xcode只可以支持iPhone手机对应iOS系统以下的真机测试.一般想要支持最新的iPhone手机系统,有两个方法. 第一.就需要更新Xcode,这一个方法有一个 ...

  3. python 安装anaconda, numpy, pandas, matplotlib 等

    如果没安装anaconda,则这样安装这些库: pip install numpy pip install pandas pip install matplotlib sudo apt-get ins ...

  4. 常见数据挖掘算法的Map-Reduce策略(1)

           大数据这个名词是被炒得越来越火了,各种大数据技术层出不穷,做数据挖掘的也跟着火了一把,呵呵,现今机器学习算法常见的并行实现方式:MPI,Map-Reduce计算框架,GPU方面,grap ...

  5. windows中检查端口占用

    在cmd中怎么输入netstat -aon|findstr "9080" 返回: UDP  0.0.0.0:8001   *.* 其中的4220为进城PID

  6. linux清空屏幕

    linux清空屏幕 clear ctrl+L reset也是真正的清空终端屏幕,这个命令执行起来有点慢,但它的兼容性显然比之前的那个好,在终端控制错乱时非常有用

  7. sql查询报错:Every derived table must have its own alias

    执行sql语句出现语法错误 Every derived table must have its own alias 翻译:每个派生表都有自己的别名

  8. 英语发音规则---/ŋ/与/ŋg/的读音区别

    英语发音规则---/ŋ/与/ŋg/的读音区别 一.总结 一句话总结: 1.位于词中间的字母组合ng,有时读作/ ŋ /,有时读作/ ŋg/? singer ['sɪŋə] n. 歌手 ringing ...

  9. 英语发音规则---Z字母

    英语发音规则---Z字母 一.总结 一句话总结:字母Z的名称zed /zed/,美式英语的称zee /zi:/,少数方言(如香港)读izzard /'izəɹd/. 1.字母Z在单词中发[z]? pu ...

  10. Logiscope学习网址

    Logiscope测试机理   http://blog.csdn.net/cmy673986/article/details/9163247 http://www.cnitblog.com/qiuya ...