Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 38918   Accepted: 15751

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

简单的最小生成树问题。

。题意要你求得最短的距离。

。1A水过


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath> using namespace std;
const int INF = 0x3f3f3f3f;
int map[105][105];//地图
int lowlen[105];//最短距离
int vist[105];//prim中用于看节点是否进树
int ans;
int n; void init()
{
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
scanf("%d", &map[i][j]);
memset(vist, 0, sizeof(vist));
memset(lowlen, 0, sizeof(lowlen));
} void prim()//prim算法
{
int temp;
ans = 0;
for(int i=1; i<=n; i++)
lowlen[i] = map[1][i];
vist[1] = -1;
for(int i=2; i<=n; i++)
{
temp = INF;
int k =0;
for(int j=1; j<=n; j++)
{
if( vist[j]!=-1 && temp>lowlen[j] )
{
temp = lowlen[j];
k = j;
}
}
vist[k] = -1;
ans += temp;
for(int j=1; j<=n; j++)
{
if(vist[j]!=-1 && lowlen[j]>map[k][j])
{
lowlen[j] = map[k][j];
}
}
}
printf("%d\n", ans);
} int main()
{
while(scanf("%d", &n)==1)
{
init();
prim();
} return 0;
}

POJ 1258:Agri-Net(最小生成树&amp;&amp;prim)的更多相关文章

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

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

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

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

  3. POJ 1258 Agri-Net(最小生成树,基础)

    题目 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<math ...

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

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

  5. poj 1258 Agri-Net【最小生成树(prime算法)】

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

  6. POJ 2485 Highways【最小生成树最大权——简单模板】

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

  7. 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 ...

  8. 最小生成树 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 // 顶点的最大个数 ( ...

  9. poj 1258 Agri-Net 解题报告

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

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

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

随机推荐

  1. dns问题,QQ打得开,网页打不开

    dns问题,QQ打得开,网页打不开 ip4 dns 改为114.114.114.114. 原因有可能是路由出错之类的.114是默认的通用ip

  2. ZeroMQ——一个轻量级的消息通信组件

    ZeroMQ是一个轻量级的消息通信组件,尽管名字中包含了"MQ",严格上来讲ZeroMQ并不是"消息队列/消息中间件".ZeroMQ是一个传输层API库, 更关 ...

  3. rust 参考的资料 转

    http://blog.csdn.net/loveisasea/article/details/46292715 rust官方学习文档: 1.http://doc.rust-lang.org/book ...

  4. GCC,LLVM,Clang编译器对比

    http://www.cnblogs.com/qoakzmxncb/archive/2013/04/18/3029105.html   在XCode中,我们经常会看到这些编译选项(如下图),有些人可能 ...

  5. c# 简单又好用的四舍五入方法

    http://www.soaspx.com/dotnet/csharp/csharp_20100415_3809.html四舍五入是软件开发中经常遇到的问题,我也在不止一个项目中用到这方面的运算:关于 ...

  6. FIREDAC用于LINUX报头文件FireDAC.VCLUI.Wait找不到

    FIREDAC用于LINUX报头文件FireDAC.VCLUI.Wait找不到 FIREDAC LINUX下面,此控件 此属性 必须设为CONSOLE.  默认值只能用于WINDOWS操作系统.

  7. python笔记5-python2写csv文件中文乱码问题

    前言 python2最大的坑在于中文编码问题,遇到中文报错首先加u,再各种encode.decode. 当list.tuple.dict里面有中文时,打印出来的是Unicode编码,这个是无解的. 对 ...

  8. 使用免安装版本在windows上手动安装PostgreSQL

    PostgreSQL支持管理员直接手动安装数据库,给用户提供了更大的方便.     1. 在PostgreSQL官方网站上下载免安装二进制的包,名字类似于postgresql-*.*.*.*-bina ...

  9. CentOS 下 LNMP 环境配置

    安装配置 Nginx 安装配置 MySQL 安装配置 PHP Nginx 与 PHP-FPM 集成 环境配置验证   LNMP 环境代表 Linux 系统下 Nginx + MySQL + PHP 网 ...

  10. STL经常使用遍历算法for_each和transform的比較

    for_each()和transform()算法比較 1)STL 算法 – 改动性算法  for_each()  copy()  copy_backward()  transform()  merge ...