Agri-Net
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 46811   Accepted: 19335

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个数之间距离形成的n*n的矩阵,求最小生成树。
 #include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm> using namespace std;
const int MAX = + ;
const int INF = ;
int g[MAX][MAX],n,dist[MAX],vis[MAX];
int main()
{
while(scanf("%d", &n) != EOF)
{
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
{
scanf("%d", &g[i][j]);
}
}
memset(dist,,sizeof(dist));
memset(vis,false,sizeof(vis));
for(int i = ; i <= n; i++)
dist[i] = g[][i]; vis[] = true;
int sum = ;
for(int i = ; i < n; i++)
{
int pos,minn = INF;
for(int j = ; j <= n; j++)
{
if(vis[j] == false)
{
if(dist[j] < minn)
{
pos = j;
minn = dist[j];
}
}
}
vis[pos] = true;
sum += minn;
for(int j = ; j <= n; j++)
dist[j] = min(dist[j],g[pos][j]);
}
printf("%d\n",sum);
}
return ;
}

POJ1258Agri-Net(prime基础)的更多相关文章

  1. Chapter2(变量和基础类型)--C++Prime笔记

    数据类型选择的准则: ①当明确知晓数值不可能为负时,选用无符号类型. ②使用int执行整数运算.在实际应用中,short常常显得太小而long一般和int有一样的尺寸.如果运算范围超过int的表示范围 ...

  2. Python基础1

    本节内容2016-05-30 Python介绍 发展史 Python 2 0r 3? 安装 Hello word程序 变量 用户输入 模块初识 .pyc? 数据类型初识 数据运算 if...else语 ...

  3. Python之路,Day1 - Python基础1

    本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语 ...

  4. python第二天基础1-1

    一.作用域 对于变量的作用域,执行声明并在内存中存在,该变量就可以在下面的代码中使用. if 1==1: name = 'wupeiqi' print name 二.三元运算 result = 值1  ...

  5. 黑马程序员_Java基础:反射机制(Reflection)总结

    ------- android培训.java培训.期待与您交流! ---------- 反射在java中有非常重大的意义,它是一种动态的相关机制,可以于运行时加载.探知.使用编译期间完全未知的clas ...

  6. Python之路【第二篇】:Python基础

    参考链接:老师 BLOG : http://www.cnblogs.com/wupeiqi/articles/4906230.html 入门拾遗 一.作用域 只要变量在内存中就能被调用!但是(函数的栈 ...

  7. python学习之路-day1-python基础1

    本节内容: Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else ...

  8. 10个经典的C语言面试基础算法及代码

    10个经典的C语言面试基础算法及代码作者:码农网 – 小峰 原文地址:http://www.codeceo.com/article/10-c-interview-algorithm.html 算法是一 ...

  9. 收集了50道基础的java面试题

    下面的内容是对网上原有的Java面试题集及答案进行了全面修订之后给出的负责任的题目和答案,原来的题目中有很多重复题目和无价值的题目,还有不少的参考答案也是错误的,修改后的Java面试题集参照了JDK最 ...

随机推荐

  1. workerman & swoole

    Socket 开发 workerman swoole swoole与phpdaemon/reactphp/workerman等纯PHP网络库的差异

  2. 如何在高并发分布式系统中生成全局唯一Id(转)

    http://www.cnblogs.com/heyuquan/p/global-guid-identity-maxId.html 又一个多月没冒泡了,其实最近学了些东西,但是没有安排时间整理成博文, ...

  3. JQuery学习笔记——JQuery基础

    #,JQuery避免名称冲突的方法 var jq = jQuery.noConfilct(); jq.ready( function(){     jq("p").hidden() ...

  4. Android Studio 2.2 来啦

    今年的 I/O 2016 Google 放出了 Android Studio 2.2 的预览版,改进了多项功能,只不过为了保证公司项目不受影响,我一般都不安装预览版的,因为预览版意味着不稳定,可能遇到 ...

  5. IOS开发之—— model最原始的封装,MJExtension加入工程(后续model都继承于它)

    DMBasicDataModel.h #import <Foundation/Foundation.h> @interface DMBasicDataModel : NSObject - ...

  6. HDU5802-windows 10-dfs+贪心

    音量减的时候,分两种,一种是减到大于目标M,另一种是减到小于M,停顿的时候可以减少最后往上加的次数,小于0的时候变成0 然后比一下这两种的最小值. /*------------------------ ...

  7. js之数组,对象,类数组对象

    许久不写了,实在是不知道写点什么,正好最近有个同事问了个问题,关于数组,对象和类数组的,仔细说起来都是基础,其实都没什么好讲的,不过看到还是有很多朋友有些迷糊,这里就简单对于定义以及一下相同点,不同点 ...

  8. this,this,再次讨论javascript中的this,超全面

    至于js中this这个东西,好多淫解释过了,看起来好高端的样子,不造你看懂了木有? 先引用比较高端的,“汤姆大叔“ 的,yes this 好了,下面加上鄙人比较挫的解释 论点: this 不是变量,不 ...

  9. Ado.net 通用访问类

    public class DbHelperSQL { private static string connString = ConfigurationManager.ConnectionStrings ...

  10. Object C学习笔记22-#define 用法

    上一篇讲到了typedef 关键字的使用,可以参考文章 Object C 学习笔记--typedef用法 .而在c中还有另外一个很重要的关键字#define. 一. #define 简介 在C中利用预 ...