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. PHP温故知新(二)

    2.安装和配置 安装这里要注意两点,是之前没有在意的: 1.将php.ini文件中的 cgi.fix_pathinfo设置为0 设置为0是为了解决一个安全漏洞,假如我们现在有这样一个URL:http: ...

  2. UltraISO(软碟通)制作安装Ubuntu系统的U盘安装盘

    1.开UltraISO(软碟通),加载要写入U盘的iso文件,注意,要求软碟通的版本是9.3以上的.如下图: 2.点击“启动光盘.写入硬盘映像: 3.开启个写入硬盘映像的窗口,硬盘驱动器那一项看清楚是 ...

  3. Redis-NoSql 概述,NoSql的优点

    全称 not only sql: 全新数据库理念:非关系型数据库: 高并发读写:海量数据的高效率存储和访问:高可扩展性和高可用性: 键值对存储:列存储:文档数据库:图形数据库: 易扩展:灵活的数据模型 ...

  4. windows 配置环境变量快捷方式

    在 Windows 设置环境变量 在环境变量中添加软件A的目录: 在命令提示框中(cmd) : 输入 path %path%;C:\A, 按下"Enter". 注意: C:\A是软 ...

  5. [ Android Memory] MAT查看图片资源

    参考: http://stackoverflow.com/questions/12709603/mat-eclipse-memory-analyzer-how-to-view-bitmaps-from ...

  6. unity3d由多个部分组成一个角色

    摘自http://forum.unity3d.com/threads/16485-quot-stitch-multiple-body-parts-into-one-character-quot So ...

  7. js清空子元素,创建新的子元素

    清空子元素 $('#region').empty(); 添加子元素 var regions = document.getElementById('region'); regions.appendChi ...

  8. [ES6] 11. String Templates

    ECMAscript 6 lets us use string templates to gain a lot more control over strings in JavaScript. var ...

  9. Junit核心——测试类(TestCase)、测试集(TestSuite)、测试运行器(TestRunner)

    首先,把这三个定义简单的说明一下: 1.测试类(TestCase):一个包含一个或是多个测试的类,在Junit中就是指的是包含那些带有@Test注解的方法的类,同一样也被称作“测试用例”; 2.测试集 ...

  10. 前端性能优化:DocumentFragments或innerHTML取代复杂的元素注入

    来源:GBin1.com 我们的浏览器执行越来越多的特性,并且网络逐渐向移动设备转移,使我们的前端代码更加紧凑,如何优化,就变得越来越重要了.前端给力的地方是可以有 许多种简单的策略和代码习惯让我们可 ...