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>
#include <string.h>
#define V 256 #define typec int
const typec inf = 0x7fffffff;
int vis[V];
typec lowc[V]; typec prim(typec cost[][V], int n)
{
int i, j, p;
typec minc, res = ;
memset(vis, , sizeof(vis));
vis[] = ;
for (i=; i<n; i++) lowc[i] = cost[][i];
for (i=; i<n; i++) {
minc = inf; p = -;
for (j=; j<n; j++)
if ( == vis[j] && minc > lowc[j]) {
minc = lowc[j]; p = j;
}
if (inf == minc) return -;
res += minc; vis[p] = ;
for (j=; j<n; j++)
if ( == vis[j] && lowc[j] > cost[p][j])
lowc[j] = cost[p][j];
}
return res;
} int matrix[V][V]; int main(){
int n,i,j;
while((scanf("%d",&n) == ) && (n>= && n<=) ){
for(i=;i<n;i++){
for(j=;j<n;j++){
scanf("%d",&matrix[i][j]);
}
} int res = prim(matrix,n);
if(- != res){
printf("%d\n",res);
}
} }

POJ1258-Agri-Net-ACM的更多相关文章

  1. Agri Net POJ1258 && Constructing Roads POJ2421

    题意,在给出的图中,使用最小花费的边,使这个图仍然连通. #include <cstdio> #include <algorithm> #include <cstring ...

  2. 【转】ACM训练计划

    [转] POJ推荐50题以及ACM训练方案 -- : 转载自 wade_wang 最终编辑 000lzl POJ 推荐50题 第一类 动态规划(至少6题, 和 必做) 和 (可贪心) (稍难) 第二类 ...

  3. acm常见算法及例题

    转自:http://blog.csdn.net/hengjie2009/article/details/7540135 acm常见算法及例题  初期:一.基本算法:     (1)枚举. (poj17 ...

  4. 转载 ACM训练计划

    leetcode代码 利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode. ...

  5. ACM常用算法及练习(1)

    ACM常用算法及练习 第一阶段:练经典常用算法,下面的每个算法给我打上十到二十遍,同时自己精简代码,因为太常用,所以要练到写时不用想,10-15分钟内打完,甚至关掉显示器都可以把程序打出来. 1.最短 ...

  6. ACM学习

    转:ACM大量习题题库   ACM大量习题题库 现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库.   US ...

  7. 【ACM/ICPC2013】POJ基础图论题简析(一)

    前言:昨天contest4的惨败经历让我懂得要想在ACM领域拿到好成绩,必须要真正的下苦功夫,不能再浪了!暑假还有一半,还有时间!今天找了POJ的分类题库,做了简单题目类型中的图论专题,还剩下二分图和 ...

  8. ACM训练计划step 2 [非原创]

    (Step2-500题)POJ训练计划+SGU 经过Step1-500题训练,接下来可以开始Step2-500题,包括POJ训练计划的298题和SGU前两章200题.需要1-1年半时间继续提高解决问题 ...

  9. ACM主要算法

    ACM主要算法ACM主要算法介绍 初期篇 一.基本算法(1)枚举(poj1753, poj2965)(2)贪心(poj1328, poj2109, poj2586)(3)递归和分治法(4)递推(5)构 ...

  10. (转载)ACM训练计划,先过一遍基础再按此拼搏吧!!!!

    ACM大量习题题库 ACM大量习题题库 现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库. USACO ht ...

随机推荐

  1. ACM1877_又一版A+B

    .这道题与2031极为相似. #include<iostream> using namespace std; void fun(int n,int r) { ]="0123456 ...

  2. 《Linear Algebra and Its Applications》-chaper3-行列式-克拉默法则

    计算线性方程组唯一解的克拉默法则:

  3. Java编程-第一个Java程序

    Java编程用到的IDE是Eclipse. 关于第一个Java程序实现的简单流程: (1)    下载eclipse (2)    File-New-Java Project 命名工程名字:Hello ...

  4. wmic

    前沿:WMIC命令在我们的工作中可以帮助我们减少对其他工具的依赖并节省我们的时间,实在是一个值得学习和研究的好东西 命令很多,这里我把网上目前能找到的较实用的一些命令整理出来,希望各位看官能找到自己需 ...

  5. linux下切割catalina.out文件,按天生成文件

    1.下载工具cronolog压缩包(http://download.csdn.net/detail/sunling_sz/8144469) 2.将文件拖放到server,不论什么文件夹都能够. 3.进 ...

  6. VS2010+Opencv-2.4.9的配置攻略

    1.下载软件 vs2010入门书籍,免积分下载   http://download.csdn.net/detail/u014112584/7325617 opencv2.4.0版本号和一些样例,免积分 ...

  7. springmvc 例

    1.结构 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaXRscWk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCM ...

  8. android 17 activity生命周期

    手机指南针传感器处于手机头部. Activity生命周期: 启动. onCreat()方法:初始化布局对象,设置监听器. onstart()方法:注册监听器. onResume():activity已 ...

  9. 设置Eclipse中文API提示信息

    准备工作:下载中文API到本机:http://download.java.net/jdk/jdk-api-localizations/jdk-api-zh-cn/publish/1.6.0/html_ ...

  10. xslt语法之---If Else

    大家都知道,XSL中是没有if else的,那么要想实现if else该怎么办呢? 其实很简单 <xsl:choose> <xsl:when test="position( ...