Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 53270   Accepted: 22140

Description

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

4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0

Sample Output

28

Source

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int map[][],minn[],n;
bool u[];
int main()
{
cin>>n;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
cin>>map[i][j];
memset(u,true,sizeof u );
memset(minn,0x7f,sizeof minn );
minn[]=;
for(int i=;i<=n;i++)
{
int k=;
for(int j=;j<=n;j++)
if(u[j]&&minn[j]<minn[k])
k=j;
u[k]=false;
for(int j=;j<=n;j++)
{
if(u[j]&&map[k][j]<minn[j])
minn[j]=map[k][j];
}
}
long long tot=;
for(int i=;i<=n;i++)
tot+=minn[i];
printf("%lld",tot); return ;
}

思路:Prim算法 蓝白点思想  minn[i]存放蓝点i与白点相连的最小边权  u[i]=True,表示顶点i还未加入到生成树中  u[i]=False,表示顶点i已加入到生成树中

最后累加一遍 Minn即可

Codevs 1078 ==Poj 1258 Agri-Net的更多相关文章

  1. 最小生成树 10.1.5.253 1505 poj 1258 http://poj.org/problem?id=1258

    #include <iostream>// poj 1258 10.1.5.253 1505 using namespace std; #define N 105 // 顶点的最大个数 ( ...

  2. poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题

    poj 1251  && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...

  3. POJ 1258 Agri-Net|| POJ 2485 Highways MST

    POJ 1258 Agri-Net http://poj.org/problem?id=1258 水题. 题目就是让你求MST,连矩阵都给你了. prim版 #include<cstdio> ...

  4. POJ 1258

    http://poj.org/problem?id=1258 今天晚上随便找了两道题,没想到两道都是我第一次碰到的类型———最小生成树.我以前并没有见过,也不知道怎么做,然后就看书,思路很容易理解 但 ...

  5. poj - 1258 Agri-Net (最小生成树)

    http://poj.org/problem?id=1258 FJ为了竞选市长,承诺为这个地区的所有农场联网,为了减少花费,希望所需光纤越少越好,给定每两个农场的花费,求出最小花费. 最小生成树. # ...

  6. POJ 1258 Agri-Net(Prim算法求解MST)

    题目链接: http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One ...

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

    链接: http://poj.org/problem?id=1258 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#probl ...

  8. Prim算法求权数和,POJ(1258)

    题目链接:http://poj.org/problem?id=1258 解题报告: #include <iostream> #include <stdio.h> #includ ...

  9. poj 1258 Agri-Net 解题报告

    题目链接:http://poj.org/problem?id=1258 题目意思:给出 n 个 farm,每个farm 之间通过一定数量的fiber 相连,问使得所有farm 直接或间接连通的 最少 ...

随机推荐

  1. C++ SFINAE

    1. 什么是SFINAE 在C++中有很多的编程技巧(Trick), SFINAE就是其中一种, 他的全义可以翻译为”匹配失败并不是一个错误(Substitution failure is not a ...

  2. 剑指 offer set 4 矩形覆盖

    总结 1. 斐波那契数列的变形题, 但是稍有隐晦, 有点意思 2. 求解 f(3) 时, 最后一块矩形可以竖着放, 也可以两块矩形横着放, 分别对应于 f(2) 和 f(1) ------------ ...

  3. Linux内核设计与实现 读书笔记 转

    Linux内核设计与实现  读书笔记: http://www.cnblogs.com/wang_yb/tag/linux-kernel/ <深入理解LINUX内存管理> http://bl ...

  4. 如何避免MVC Model First 设计时添加的DataAnnotation被覆盖掉

    结合多方资料做一系统,发现Code First中所有代码要自己写,无法自动生成(暂时没有找到方法,有知道的大能,给指点一下,好像在NuGet中有一个插件可以直接从数据库中生成Code First所需类 ...

  5. Qt界面美化 QSS

    目前发现在Qt-Design中右击控件,可以选择Change StyleSheet ------------------------以下总结不太对 刚接触Qt,发现Qt Design无法对每个控件进行 ...

  6. 结合源码看nginx-1.4.0之nginx全局变量ngx_cycle初始化详解

    目录 0. 摘要 1. ngx_cycle_t结构设计 2. ngx_cycle_t数据结构 3. nginx全局变量ngx_cycle初始化 4. 小结 5. 参考资料 0. 摘要 Nginx核心的 ...

  7. LocalActivityManager的内部机制

    LocalActivityManager内部机制的核心在于,它使用了主线程对象mActivityThread来装载指定的Activity.注意,这里是装载,而不是启动,这点很重要. 所谓的启动,一般是 ...

  8. Android Activity的启动过程

    文章编辑的太长了,请移步我的csdn博客:http://blog.csdn.net/xyh269 Android Activity的启动过程原文链接:http://blog.csdn.net/xyh2 ...

  9. iOS 画图讲解2

    1.图片水印 //layer上下文只能显示在drawRect里 //当开启上下文时,绘制图形即可在viewDidLoad中实现 //位图的上下文 //UIGraphicsBeginImageConte ...

  10. Oracle 经典语法(五)

    1. 哪些部门的人数比20 号部门的人数多.SELECT DEPTNO,COUNT(*) FROM EMP     GROUP BY DEPTNO      HAVING COUNT(*) >  ...