Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 44373   Accepted: 18127

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
 #include <stdio.h>
#define INF 100000
int n;
int map[][];
int dis[],vis[];
int Prim(){
for(int i=;i<n;i++){
vis[i]=;
dis[i]=INF;
}
dis[]=; for(int i=;i<n;i++){
int min=INF;
int p;
for(int j=;j<n;j++){
if(!vis[j]&&min>dis[j]){
min=dis[j];
p=j;
}
}
vis[p]=;
for(int j=;j<n;j++){
if(!vis[j]&&dis[j]>map[p][j]){
dis[j]=map[p][j];
}
}
}
for(int i=;i<n;i++){
dis[]+=dis[i];
}
return dis[];
}
int main() {
while(~scanf("%d",&n)){
for(int i=;i<n;i++){
for(int j=;j<n;j++){
scanf("%d",&map[i][j]);
}
}
int min=Prim();
printf("%d\n",min);
}
return ;
}

Agri-Net - poj 1258 (Prim 算法)的更多相关文章

  1. Highways - poj 2485 (Prim 算法)

      Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24383   Accepted: 11243 Description T ...

  2. Truck History - poj 1789 (Prim 算法)

      Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20884   Accepted: 8075 Description Ad ...

  3. Agri-Net POJ - 1258 prim

    #include<iostream> #include<cstdio> #include<cstring> using namespace std; ; #defi ...

  4. POJ 1258 Agri-Net(Prim算法)

    题意:n个农场,求把所有农场连接起来所需要最短的距离. 思路:prim算法 课本代码: //prim算法 #include<iostream> #include<stdio.h> ...

  5. POJ 1258 Agri-Net(Prim)

    题目网址:http://poj.org/problem?id=1258 题目: Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

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

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

  7. POJ 1258 Agri-Net (Prim&Kruskal)

    题意:FJ想连接光纤在各个农场以便网络普及,现给出一些连接关系(给出邻接矩阵),从中选出部分边,使得整个图连通.求边的最小总花费. 思路:裸的最小生成树,本题为稠密图,Prim算法求最小生成树更优,复 ...

  8. POJ 1258:Agri-Net(最小生成树&amp;&amp;prim)

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

  9. poj 1258 Agri-Net(Prim)(基础)

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

随机推荐

  1. unity3d 场景配置文件生成代码

    using UnityEngine; using UnityEditor; using System.IO; using System; using System.Text; using System ...

  2. Matrix.LookAtLH()和Matrix.LookAtRH()所表达的涵义

    function lookAtLH(eye:Vector3D, at:Vector3D, up:Vector3D) 一个摄像机矩阵可有由三个部分组成:摄像机位置.目标位置以及摄像机上下方.对应的就是上 ...

  3. PHP开发环境配置系列(四)-XAMPP常用信息

    PHP开发环境配置系列(四)-XAMPP常用信息 博客分类: PHP开发环境配置系列 xamppphp 完成了前面三篇后(<PHP开发环境配置系列(一)-Apache无法启动(SSL冲突)> ...

  4. Python学习笔记——基本数据结构

    列表list List是python的一个内置动态数组对象,它的基本使用方式如下: shoplist = ['apple', 'mango', 'carrot', 'banana'] print 'I ...

  5. 用android连小米手机做profiling

    安装失败 Installation failed with message Failed to finalize session http://www.jianshu.com/p/fea7d96385 ...

  6. ELKStack日志离线系统

    通过Filebeat抽取数据到logstash中,转存到ElasticSearch中,最后通过Kibana进行展示 https://www.ibm.com/developerworks/cn/open ...

  7. 转: NetBean 远程开发的好文2 --> 工欲善其事,必先利其器系列--Netbeans之远程开发

    转自:  http://www.cnblogs.com/zuoca/archive/2012/07/09/Remote_Development_With_Netbeans_origin.html 实践 ...

  8. Binder与interface

    在Interface中,asBinder函数涌来将服务类接口类型转换为IBinder类型: 相反的,asInterface函数用来将Ibinder类型转换为服务接口类型

  9. 设计模式——浅复制VS深复制

    背景 在学习原型模式的时候,採用了一个差别与其它模式的新方法.採用了"克隆(Clone)方法.通过实现ICloneable接口中的Clone()方法来达到克隆的目的. 代码实现过程中,存在了 ...

  10. linux生成指定大小的文件(转)

    # dd if=/dev/zero of=50M.file bs=1M count=50在当前目录下生成一个50M的文件 虚拟块设备文件更通用的名称是硬盘镜像文件(Hard Disk Image),但 ...