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. noip数论复习总结

    (上不了p站我要死了,侵权度娘背锅) 勉勉强强算是把数论复习的差不多了. 总结一下吧. 其实数论的知识大部分是结合在一起的,勉强分类总结 组合数 求法 组合数的求法根据不同情况选用不同的方法 2.3都 ...

  2. GIF工具 | 分享几个Gif相关工具

    文章目录 LICEcap GiFResizer LICEcap LICEcap 是一款屏幕录制工具,支持导出GIF动画图片格式,轻量级.使用简单,录制过程中可以随意改变录屏范围. 下载地址: 官方地址 ...

  3. JavaScrip book

     1.<JavaScript: The Good Parts>中文版:<JavaScript语言精粹>2.<Professional JavaScript for Web ...

  4. MySQL不能启动 Can't start server : Bind on unix socket: Permission denied

    转载博客地址:http://www.linuxidc.com/Linux/2010-04/25709.htm MySQL服务器突然不能启动,查看最后的启动日志如下: 080825 09:38:04 m ...

  5. django book多站点学习

    多个站点 Django 的多站点系统是一种通用框架,它让你可以在同一个数据库和同一个Django项目下操作多个网站. 这是一个抽象概念,理解起来可能有点困难,因此我们从几个让它能派上用场的实际情景入手 ...

  6. Node.js 网页爬虫再进阶,cheerio助力

    任务还是读取博文标题. 读取app2.js // 内置http模块,提供了http服务器和客户端功能 var http=require("http"); // cheerio模块, ...

  7. 使用WIFI连接android进行调试和adb操作

    本人需要wifi连接android进行调试的原因主要是要经常用到IDA pro进行调试,但手头有的IDA Pro版本只是windows的,开发可能更多用Mac OS X了,来回拔插.调试很不方便,所以 ...

  8. Oracle学习——扫盲篇

    前言 近期这几天一直在与Oracle数据库打交道.因为之前对Oracle的学习并不深入,仅仅是把Oracle当成一个数据源去使用.非常多东西了解的不是非常深,比方.数据库.数据库实例.表空间.用户.表 ...

  9. ANT使用 - 用for和foreach的方法遍历一个文件夹,查找到某个文件并删除

    转自:http://www.cnblogs.com/QAZLIU/p/3732329.html?utm_source=tuicool&utm_medium=referral build.xml ...

  10. Win7如何配置java环境变量,运行环境

    直接运行eclipse,弹出错误提示.   1 确保你安装了JDK,安装之后文件夹示例如下(jdk1.x.x取决于你安装的JDK版本)   2 系统,高级系统设置,高级,环境变量新建一个JAVA_HO ...