一、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代码

  1. import java.util.Scanner;
  2.  
  3. public class Test {
  4. public static int prim(int[][] a, int count) {
  5. int sum = 0;
  6. int i, j, k;
  7. int[] lowcost = new int[count]; //保存相关定点间的权值
  8. int[] adjvex = new int[count]; //保存相关定点的下标
  9. for (i = 0; i < count; i++) {
  10. lowcost[i] = a[0][i];
  11. adjvex[i]=0;
  12. }
  13. for (i = 1; i < count; i++) {
  14. j=0;
  15. while(lowcost[j]==0){
  16. j++;
  17. }
  18. for (k = 1; k < count; k++) {
  19. if ((lowcost[k]!=0) && (lowcost[k] < lowcost[j])) {
  20. j = k;
  21. }
  22. }
  23. sum += lowcost[j];
  24. lowcost[j] = 0;
  25. for (k = 1; k < count; k++) {
  26. if (lowcost[k]!=0 && (a[j][k] < lowcost[k])) {
  27. {
  28. lowcost[k] = a[j][k];
  29. adjvex[k] = j;
  30. }
  31. }
  32. }
  33. }
  34. return sum;
  35. }
  36. public static void main(String[] args) {
  37.  
  38. Scanner cin=new Scanner(System.in);
  39. int n;
  40. while((n=cin.nextInt())!=-1){
  41. int vertexNumber = n;
  42. int arr[][] = new int[vertexNumber][vertexNumber];
  43. for (int i = 0; i < vertexNumber; i++) {
  44. for (int j = 0; j < vertexNumber; j++) {
  45. arr[i][j]=cin.nextInt();
  46. }
  47. }
  48. System.out.println(prim(arr,n));
  49. n=-1;
  50. }
  51. }
  52. }

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

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. springboot工程自动生成工具

    1 springboot工程自动生成网址 http://start.spring.io/ 2 工具 Spring Boot CLI

  2. zookeepeer ID生成器 (一)

    目录 写在前面 1.1. ZK 的分布式命名服务 1.1.1. 分布式 ID 生成器的类型 UUID方案 1.1.2. ZK生成分布式ID 写在最后 疯狂创客圈 亿级流量 高并发IM 实战 系列 疯狂 ...

  3. cocos2dx使用cocostudio导出的scene

    local uilocal function createLayerUI() if not ui then ui=cc.Layer:create(); createLayerUI=nil; end r ...

  4. Django 之 缓存机制

    Django 缓存机制 缓存介绍 在动态网站中,用户所有的请求,服务器都会去数据库中进行相应的增,删,查,改,渲染模板,执行业务逻辑,最后生成用户看到的页面. 当一个网站的用户访问量很大的时候,每一次 ...

  5. Django 之 admin组件使用&源码解析

    admin组件使用 Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.可以在项目的 settings.py 中的 INSTALLED ...

  6. linux c编程:进程控制(一)

    一个进程,包括代码.数据和分配给进程的资源.fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程, 也就是两个进程可以做完全相同的事,但如果初始参数或者传入的变量不同,两个进程也可以做不同 ...

  7. Redis持久化——AOF(二)

    核心知识点: 1.AOF:以独立日志的方式记录写命令,重启时再执行命令.与RDB不同的是解决数据持久化的实时性,可以记录所有写操作. 2.AOF工作流程:写入命令.文件同步.文件重写.文件加载. 3. ...

  8. UML建模:学习笔记(1)

    UML:学习笔记(1) 事物 结构事物 类: 接口: 协作:(定义元素之间的相互作用) 用例:(在系统外部和系统交互的人) 组件:(描述物理系统的一部分) 节点:(一个节点可以被定义为运行时存在的物理 ...

  9. js之语句的一些需要注意的事情

    1.delete运算符是用来删除一个对象的 属性,但有一点需要注意:使用var声明的变量虽为全局变量,单不是全局对象的属性,不可以用delete删除,而不用var直接声明的全局变量而直接赋值的为全局对 ...

  10. Havel-Hakimi定理 POJ1659

    对于图的所有顶点,计算出每个顶点的度,度序列.给定一个序列判断序列是否可图. #include<cstdio> #include<algorithm> #include< ...