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. TOSCA自动化测试工具--new Project

    1.在默认页面点击Project 进入new project页面 2.第一步出来的页面上点击new 3. 第2步弹出的页面上选择路径,project 名 3.OK之后就创建好了

  2. 如何建立DB2分区数据库?(转)

    欢迎和大家交流技术相关问题:邮箱: jiangxinnju@163.com博客园地址: http://www.cnblogs.com/jiangxinnjuGitHub地址: https://gith ...

  3. python requests的使用说明

    #GET参数实例 requests.get('http://www.dict.baidu.com/s', params={'wd': 'python'}) #或 url = 'http://www.b ...

  4. python3_Logging模块详解

    python的logging模块提供了通用的日志系统,可以方便第三方模块或应用使用. 简单使用 import logging # logging.config.fileConfig("./l ...

  5. H5 播放视频常见bug及解决方案

    本文摘自:本文来自“小时光茶社(Tech Teahouse)”公众号 原文:https://mp.weixin.qq.com/s/MM5ZwCiWLAeHalsNYMImnw 1. 自动播放问题 通过 ...

  6. C#创建类,方法,接口,字段 的 默认类型

    1.在namespace中的类.接口默认是internal类型的,也可以显示的定义为public类型2.在一个类里面,属性和方法默认是private的,可以显示的定义为public.private.p ...

  7. mysql5.6创建索引导致锁表阻塞查询

    结论:添加索引时,若果有对该表的慢查询,会导致索引添加延时等待   添加索引语句:alter table tb_name add index idx_xx(col_name);   执行添加索引的SQ ...

  8. markdown工作随笔总结

    1. 锚点 (使用方法和链接很像) ## 目录 1. [命名](#命名) ....... **[返回顶部](#目录)** ## 命名 ###命名原则 可以从返回顶部回到目录,也可以点击目录的命名跳到命 ...

  9. hdu 2896:病毒侵袭

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission ...

  10. 如何设置hyper-v下的ubuntu虚拟机分辨率

    1.登陆ubuntu虚拟机 2.vi /etc/default/grub,改变如下内容: 改变前: GRUB_CMDLINE_LINUX_DEFAULT="quiet splash" ...