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. C语言提供的位运算符

      运算符 含义 描述 & 按位与 如果两个相应的二进制位都为1,则该位的结果值为1,否则为0 | 按位或 两个相应的二进制位中只要有一个为1,该位的结果值为1 ^ 按位异或 若参加运算的两个 ...

  2. variable 'xxx' unsafe in 'case'的处理

    问题描述: case get(?Player_LoopTaskInfo) of {TargetCnt, TaskStar, TaskExp} -> ok; _ -> throw(" ...

  3. MySQL学习指引

    mysql指引 1,mysql基本安装 2,mysql多实例安装与维护 3,备份恢复 备份数据库 分备数据库 分备表 恢复数据库

  4. [android界面]android中src和background区别——前景与背景

    ImageView中XML属性src和background的区别: background会根据ImageView组件给定的长宽进行拉伸,而src就存放的是原图的大小,不会进行拉伸.src是图片内容(前 ...

  5. Oracle数据库,数字强制显示2位小数(转)

    Oracle数据库,数字强制显示2位小数 在银行.财务等对数字要求敏感的系统中,数字的显示一般有着严格的要求.今遇到一个需求,如题,要求将数字以两位小数的格式显示,如果没有小数,则强制显示为0.例如: ...

  6. [C#] 走进异步编程的世界 - 开始接触 async/await(转)

    原文链接:http://www.cnblogs.com/liqingwen/p/5831951.html 走进异步编程的世界 - 开始接触 async/await 序 这是学习异步编程的入门篇. 涉及 ...

  7. Android 获取手机Mac地址,手机名称

    /** * 获取手机mac地址<br/> * 错误返回12个0 */ public static String getMacAddress(Context context) { // 获取 ...

  8. 关于ARP攻击的原理以及在Kali Linux环境下的实现

    关于ARP攻击的原理以及在Kali Linux环境下的实现 全文摘要 本文讲述内容分为两部分,前半部分讲述ARP协议及ARP攻击原理,后半部分讲述在Kali Linux环境下如何实现ARP攻击以及AR ...

  9. Anaconda安装更新库

    平台:win64+anaconda 1. 如何查看已安装的库 打开 Anaconda Command Prompt ,在命令提示符窗口中输入以下命令: pip list # 或者 conda list ...

  10. 如何部署Iveely.Computing分布式实时计算系统

    Iveely.Computing是参考Storm的分布式实时计算系统的部分原理,用纯Java实现的轻量级.迷你型,适合于搜索引擎的实时计算系统, Iveely 搜索引擎是一款基于Iveely.Comp ...