Agri-Net
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 41230   Accepted: 16810

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 <cstdio>
  2. #include<cstring>
  3. #include <queue>
  4. #include <assert.h>
  5. using namespace std;
  6.  
  7. int blen;
  8. int d[102][102];
  9. int n;
  10. bool vis[102];
  11. typedef pair<int,int> P;
  12. int prim(){
  13. memset(vis,0,sizeof(vis));
  14. priority_queue<P,vector<P>,greater<P> >que;
  15. int num=1;
  16. int ans=0;
  17. for(int i=1;i<n;i++){
  18. que.push(P(d[0][i],i));
  19. }
  20. vis[0]=true;
  21. while(num<n&&!que.empty()){
  22. int t=que.top().second;
  23. int td=que.top().first;
  24. que.pop();
  25. if(vis[t])continue;
  26. vis[t]=true;
  27. ans+=td;
  28. num++;
  29. for(int i=0;i<n;i++){
  30. if(!vis[i]){
  31. que.push(P(d[t][i],i));
  32. }
  33. }
  34. }
  35. return ans;
  36. }
  37. int main(){
  38. while(scanf("%d",&n)==1){
  39. for(int i=0;i<n;i++){
  40. for(int j=0;j<n;j++){
  41. scanf("%d",d[i]+j);
  42. }
  43. }
  44. int ans=prim();
  45. printf("%d\n",ans);
  46. }
  47. return 0;
  48. }

  

poj 1258 Agri-Net 最小生成树 prim算法+heap不完全优化 难度:0的更多相关文章

  1. 快速切题 poj 2485 Highways prim算法+堆 不完全优化 难度:0

    Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23033   Accepted: 10612 Descri ...

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

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

  3. 数据结构代码整理(线性表,栈,队列,串,二叉树,图的建立和遍历stl,最小生成树prim算法)。。持续更新中。。。

    //归并排序递归方法实现 #include <iostream> #include <cstdio> using namespace std; #define maxn 100 ...

  4. 最小生成树Prim算法(邻接矩阵和邻接表)

    最小生成树,普利姆算法. 简述算法: 先初始化一棵只有一个顶点的树,以这一顶点开始,找到它的最小权值,将这条边上的令一个顶点添加到树中 再从这棵树中的所有顶点中找到一个最小权值(而且权值的另一顶点不属 ...

  5. 最小生成树—prim算法

    最小生成树prim算法实现 所谓生成树,就是n个点之间连成n-1条边的图形.而最小生成树,就是权值(两点间直线的值)之和的最小值. 首先,要用二维数组记录点和权值.如上图所示无向图: int map[ ...

  6. Highways POJ-1751 最小生成树 Prim算法

    Highways POJ-1751 最小生成树 Prim算法 题意 有一个N个城市M条路的无向图,给你N个城市的坐标,然后现在该无向图已经有M条边了,问你还需要添加总长为多少的边能使得该无向图连通.输 ...

  7. SWUST OJ 1075 求最小生成树(Prim算法)

    求最小生成树(Prim算法) 我对提示代码做了简要分析,提示代码大致写了以下几个内容 给了几个基础的工具,邻接表记录图的一个的结构体,记录Prim算法中最近的边的结构体,记录目标边的结构体(始末点,值 ...

  8. 图论算法(五)最小生成树Prim算法

    最小生成树\(Prim\)算法 我们通常求最小生成树有两种常见的算法--\(Prim\)和\(Kruskal\)算法,今天先总结最小生成树概念和比较简单的\(Prim\)算法 Part 1:最小生成树 ...

  9. 最小生成树,Prim算法与Kruskal算法,408方向,思路与实现分析

    最小生成树,Prim算法与Kruskal算法,408方向,思路与实现分析 最小生成树,老生常谈了,生活中也总会有各种各样的问题,在这里,我来带你一起分析一下这个算法的思路与实现的方式吧~~ 在考研中呢 ...

随机推荐

  1. RabbitMQ、Memcache、Redis RabbitMQ

    RabbitMQ 解释RabbitMQ,就不得不提到AMQP(Advanced Message Queuing Protocol)协议. AMQP协议是一种基于网络的消息传输协议,它能够在应用或组织之 ...

  2. https://www.cnblogs.com/skywang12345/category/455711.html

    https://www.cnblogs.com/skywang12345/category/455711.html

  3. 如何用纯 CSS 创作一个摇摇晃晃的 loader

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览.https://codepen.io/comehope/pen/oyJvpe 可交互视频 此 ...

  4. python3执行js之pyexecjs

    执行js的三种方法:1.阅读js代码,将之转成python2.找到js代码,用python第三方库执行相关代码 python2-pyv8 python3-pyexecjs3.用selenium驱动浏览 ...

  5. [代码解析]Mask R-CNN介绍与实现(转)

    文章来源 DFann 版权声明:如果你觉得写的还可以,可以考虑打赏一下.转载请联系. https://blog.csdn.net/u011974639/article/details/78483779 ...

  6. Ignatius and the Princess III - 拆分数-动态规划(dp)

    ---恢复内容开始--- 2017-08-10 20:00:45 writer:pprp 拆分数: 把正整数n拆分成k个正整数之和的方案数: 问题转换:将1转化为2 1.把n表示成m个正整数之和的方案 ...

  7. ThinkPHP开发笔记-用户登录注册

    1.修改模块配置,Application/当前模块名/Conf/config.php <?php return array( //数据库配置信息 'DB_TYPE' => 'mysql', ...

  8. Linux安装jdk、删除Open jdk

    1.将jdk解压安装完成后,在bin目录下查看当前jdk的版本号 命令: ./java -version 2.编辑修改配置 1. 修改profile文件 进入命令:   vi  /etc/profil ...

  9. Sql索引

    1.为什么要给表加上主键?建表的时候都会为表加上主键, 在某些关系数据库中, 如果建表时不指定主键,数据库会拒绝建表的语句执行. 一个没加主键的表,并不能被称之为「表」.一个没加主键的表,它的数据无序 ...

  10. Spring事物原理完全解析

    事务是什么?了解事务的原理吗?说下Spring的事务原理,能自己实现Spring事务原理吗?先自我检测下这些知识掌握了吗.那么接下来一起看下与Spring相关的事务 概念 事务具有ACID特性. 是指 ...