Agri-Net
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 44827   Accepted: 18351

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

  1. 4
  2. 0 4 9 21
  3. 4 0 8 17
  4. 9 8 0 16
  5. 21 17 16 0

Sample Output

  1. 28
    做题做的头疼,水一道:
  1. #include<stdio.h>
  2. #include<string.h>
  3. #define inf 0x3f3f3f
  4. #define MAX 110
  5. int map[MAX][MAX],low[MAX],vis[MAX];
  6. int t;
  7. void prime()
  8. {
  9. int i,j,next,min,lowdis=0;
  10. memset(vis,0,sizeof(vis));
  11. for(i=1;i<=t;i++)
  12. {
  13. low[i]=map[1][i];
  14. }
  15. vis[1]=1;
  16. for(i=1;i<t;i++)
  17. {
  18. min=inf;
  19. for(j=1;j<=t;j++)
  20. {
  21. if(!vis[j]&&min>low[j])
  22. {
  23. min=low[j];
  24. next=j;
  25. }
  26. }
  27. lowdis+=min;
  28. vis[next]=1;
  29. for(j=1;j<=t;j++)
  30. {
  31. if(!vis[j]&&low[j]>map[next][j])
  32. {
  33. low[j]=map[next][j];
  34. }
  35. }
  36. }
  37. printf("%d\n",lowdis);
  38. }
  39. int main()
  40. {
  41. int n,m,j,i,s;
  42. while(scanf("%d",&t)!=EOF)
  43. {
  44. for(i=1;i<=t;i++)
  45. {
  46. for(j=1;j<=t;j++)
  47. {
  48. if(i==j)
  49. map[i][j]=0;
  50. else
  51. map[i][j]=inf;
  52. }
  53. }
  54. for(i=1;i<=t;i++)
  55. {
  56. for(j=1;j<=t;j++)
  57. {
  58. scanf("%d",&n);
  59. map[i][j]=n;
  60. }
  61. }
  62. prime();
  63. }
  64. return 0;
  65. }

  

poj 1258 Agri-Net【最小生成树(prime算法)】的更多相关文章

  1. POJ 1258 Agri-Net(最小生成树,模板题)

    用的是prim算法. 我用vector数组,每次求最小的dis时,不需要遍历所有的点,只需要遍历之前加入到vector数组中的点(即dis[v]!=INF的点).但其实时间也差不多,和遍历所有的点的方 ...

  2. POJ2395 最小生成树 - Prime算法

    题目: Out of Hay Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Description The ...

  3. 最小生成树 prime算法 UVALive - 6437

    题目链接:https://vjudge.net/contest/241341#problem/D 这里有多个发电站,需要求出所有点都和发电站直接或间接相连的最小代价,那么就是求出最小生成树的问题了,有 ...

  4. 最小生成树prime算法模板

    #include<stdio.h> #include<string.h> using namespace std; int map[505][505]; int v, e; i ...

  5. POJ 1258 Agri-Net(最小生成树 Prim+Kruskal)

    题目链接: 传送门 Agri-Net Time Limit: 1000MS     Memory Limit: 10000K Description Farmer John has been elec ...

  6. POJ 1258 Agri-Net (最小生成树)

    Agri-Net 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/H Description Farmer John has be ...

  7. poj 1287 Networking【最小生成树prime】

    Networking Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7321   Accepted: 3977 Descri ...

  8. hdoj 1233 还是畅通工程---最小生成树---prime算法

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1233 可以用Kruskal来做,不过当图的边比较稠密的时候用prime会更快一些. AC代码:296MS ...

  9. hdoj 1863 畅通工程 最小生成树---prime算法

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=1863 注意有可能出现无法生成树的情况. #include <iostream> #inclu ...

随机推荐

  1. OC - 18.监听iPhone的网络状态

    使用系统的方法来监听网络状态 系统的方法是通过通知机制来实现网络状态的监听 实现网络状态监听的步骤 定义Reachability类型的成员变量来保存网络的状态 @property (nonatomic ...

  2. 【转】NHibernate入门教程

    开源框架完美组合之Spring.NET + NHibernate + ASP.NET MVC + jQuery + easyUI 中英文双语言小型企业网站Demo 摘要: 热衷于开源框架探索的我发现A ...

  3. VMware虚拟机中如何安装VMWare-Tools详解

    VMware虚拟机中如何安装VMWare-Tools详解 好处:可以支持图形界面,可以支持共享文件功能等 VMware虚拟机中如何配置显 VMware作为一款虚拟机利器,很多人都利用它来实现Linux ...

  4. 为SQL Server表中的列添加/修改/删除注释属性(sp_addextendedproperty、sp_updateextendedproperty、sp_dropextendedproperty)

    本篇基本完全参考:sql--sp_addextendedproperty和sp_updateextendedproperty (Transact-SQL) 三个存储过程用法一样,以sp_addexte ...

  5. HTML5 程序设计笔记(二)

    Canvas API 1.HTML5 Canvas 概述 1.1 历史 Canvas的概念最初是由苹果公司提出的,用于在Mac OS X WebKit中创建控制板部件(dashboard widget ...

  6. Maven项目的目录结构

    刚接触Maven没多长时间,实习时在的小公司很不规范,所有web项目都是dynamic web project搭建,没有用到项目构建,管理工具,导致了非常多的重复工作与低效. 先来看看Maven的功能 ...

  7. BOM 之 screen history

    /*    avaiHeight // 屏幕的像素高度减去系统部件高度之后的值    var ah = screen.availHeight;     alert(ah); */    /* avai ...

  8. js调用.net后台事件,和后台调用前台等方法以及js调用服务器控件的方法

    http://blog.csdn.net/deepwishly/article/details/6670942  ajaxPro.dll基础教程(前台调用后台方法,后台调用前台方法) 1. javaS ...

  9. php Imagick库readImage()报Postscript delegate failed 解决方法(失效)

    需要安装 ghostscript http://www.ghostscript.com/download/gsdnld.html

  10. Android中UI线程与后台线程交互设计的5种方法

    我想关于这个话题已经有很多前辈讨论过了.今天算是一次学习总结吧. 在android的设计思想中,为了确保用户顺滑的操作体验.一 些耗时的任务不能够在UI线程中运行,像访问网络就属于这类任务.因此我们必 ...