http://poj.org/problem?id=1258

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

题目大意:有n个农场,已知这n个农场都互相相通,有一定的距离,现在每个农场需要装光纤,问怎么安装光纤能将所有农场都连通起来,并且要使光纤距离最小,输出安装光纤的总距离
解题思路:又是一个最小生成树,因为给出了一个二维矩阵代表他们的距离,直接算prim就行了。
#include <iostream>
#include <stdio.h>
#include <string.h>
#define INF 0x3f3f3f3f
using namespace std;
int map[][];
int n,dis[],v[];
void prim()
{
int min,sum=,k;
for(int i=; i<=n; i++)
{
v[i]=;
dis[i]=INF;
}
for(int i=; i<=n; i++)
dis[i]=map[][i];
v[]=;
for(int j=; j<n; j++)
{
min=INF;
for(int i=; i<=n; i++)
{
if(v[i]==&&dis[i]<min)
{
k=i;
min=dis[i];
}
}
sum+=min;
v[k]=;
for(int i=; i<=n; i++)
{
if(v[i]==&&map[k][i]<dis[i])
{
dis[i]=map[k][i];
}
}
}
cout<<sum<<endl;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==) break;
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
cin>>map[i][j];
}
}
prim();
}
return ;
}
												

POJ1258:Agri-Net(最小生成树模板题)的更多相关文章

  1. POJ 1258:Agri-Net Prim最小生成树模板题

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

  2. O - 听说下面都是裸题 (最小生成树模板题)

    Economic times these days are tough, even in Byteland. To reduce the operating costs, the government ...

  3. 最小生成树模板题-----P3366 【模板】最小生成树

    题目描述 如题,给出一个无向图,求出最小生成树,如果该图不连通,则输出orz 输入格式 第一行包含两个整数N.M,表示该图共有N个结点和M条无向边.(N<=5000,M<=200000) ...

  4. POJ 1789 Truck History (Kruskal最小生成树) 模板题

    Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for v ...

  5. 最小生成树模板题POJ - 1287-prim+kruskal

    POJ - 1287超级模板题 大概意思就是点的编号从1到N,会给你m条边,可能两个点之间有多条边这种情况,求最小生成树总长度? 这题就不解释了,总结就算,prim是类似dijkstra,从第一个点出 ...

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

  7. POJ 1287 Networking (最小生成树模板题)

    Description You are assigned to design network connections between certain points in a wide area. Yo ...

  8. 继续畅通工程--hdu1879(最小生成树 模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1879 刚开始么看清题  以为就是n行  后来一看是n*(n-1)/2行   是输入错误  真是够够的 #incl ...

  9. 最小生成树模板题 hpu 积分赛 Vegetable and Road again

    问题 H: Vegetable and Road again 时间限制: 1 Sec 内存限制: 128 MB 提交: 19 解决: 8 题目描述 修路的方案终于确定了.市政府要求任意两个公园之间都必 ...

随机推荐

  1. Linux设备驱动剖析之SPI(二)

    957至962行,一个SPI控制器用一个master来描述.这里使用SPI核心的spi_alloc_master函数请求分配master.它在drivers/spi/spi.c文件中定义: struc ...

  2. Intellij 部署项目java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    报错信息: org.apache.catalina.core.StandardContext.listenerStart Error configuring application listener ...

  3. rman 中遇到 ORA-01861

    RMAN> run{ 2> sql 'alter session set nls_date_format="yyyy-mm-dd hh24:mi:ss"'; 3> ...

  4. Elasticsearch学习之快速入门案例

    1. document数据格式 面向文档的搜索分析引擎 (1)应用系统的数据结构都是面向对象的,复杂的(2)对象数据存储到数据库中,只能拆解开来,变为扁平的多张表,每次查询的时候还得还原回对象格式,相 ...

  5. Spring Cloud Eureka 服务治理机制

     服务提供者         服务提供者在启动的时候会通过发送REST请求的方式将自己注册到Eureka Server上,同时带上了自身服务的一些元数据信息.Eureka Server 接收到这个RE ...

  6. redis -clock_gettime问题

    /home/wm/redis-/deps/jemalloc/src/nstime.c:: undefined reference to `clock_gettime' 这个错误 解决思路如下 .查找实 ...

  7. 《modern-php》 - 阅读笔记 - 最佳实践

    过滤.验证和转义数据 过滤数据 不要相信任何外部数据! 常见的有以下几种数据需要过滤:HTML,SQL查询,用户提交的信息(邮件地址.电话号码.身份证) HTML htmlentities() HTM ...

  8. HDU 1243 反恐训练营(最长公共序列)

    反恐训练营 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submiss ...

  9. hdu 2444 The Accomodation of Students 【二分图匹配】

    There are a group of students. Some of them may know each other, while others don't. For example, A ...

  10. Mapreduce其他部分

    1.hadoop的压缩codec Codec为压缩,解压缩的算法实现. 在Hadoop中,codec由CompressionCode的实现来表示.下面是一些实现: 可分割性:可分割与不可分割的区别:文 ...