Agri-Net
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 46319   Accepted: 19052

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

Source

 
prim求最小生成树
prim和dijkstra的代码只有更新dis数组这一块是不同的,prim中dis[i]表示的是i到当前生成树的距离,dijkstra中dis[i]表示的是i到源点s的距离
/*
ID: LinKArftc
PROG: 1258.cpp
LANG: C++
*/ #include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const double e = exp(1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll; const int maxn = ; int mp[maxn][maxn];
int dis[maxn];
bool vis[maxn];
int n; int prim() {
int ret = ;
for (int i = ; i <= n; i ++) dis[i] = mp[i][];
dis[] = ;
vis[] = true;
int ii = ;
for (int i = ; i <= n; i ++) {
int mi = inf;
for (int j = ; j <= n; j ++) {
if (!vis[j] && dis[j] < mi) {
mi = dis[j];
ii = j;
}
}
vis[ii] = true;
ret += dis[ii];
for (int j = ; j <= n; j ++) {
if (!vis[j] && dis[j] > mp[j][ii]) dis[j] = mp[j][ii];
}
}
return ret;
} int main() { //input;
while (~scanf("%d", &n)) {
memset(mp, 0x3f, sizeof(mp));
memset(vis, , sizeof(vis));
for (int i = ; i <= n; i ++) {
for (int j = ; j <= n; j ++) scanf("%d", &mp[i][j]);
}
printf("%d\n", prim());
} return ;
}

POJ1258 (最小生成树prim)的更多相关文章

  1. 最小生成树—prim算法

    最小生成树prim算法实现 所谓生成树,就是n个点之间连成n-1条边的图形.而最小生成树,就是权值(两点间直线的值)之和的最小值. 首先,要用二维数组记录点和权值.如上图所示无向图: int map[ ...

  2. 数据结构代码整理(线性表,栈,队列,串,二叉树,图的建立和遍历stl,最小生成树prim算法)。。持续更新中。。。

    //归并排序递归方法实现 #include <iostream> #include <cstdio> using namespace std; #define maxn 100 ...

  3. 邻接矩阵c源码(构造邻接矩阵,深度优先遍历,广度优先遍历,最小生成树prim,kruskal算法)

    matrix.c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include < ...

  4. 最小生成树Prim算法(邻接矩阵和邻接表)

    最小生成树,普利姆算法. 简述算法: 先初始化一棵只有一个顶点的树,以这一顶点开始,找到它的最小权值,将这条边上的令一个顶点添加到树中 再从这棵树中的所有顶点中找到一个最小权值(而且权值的另一顶点不属 ...

  5. 转载:最小生成树-Prim算法和Kruskal算法

    本文摘自:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/30/2615542.html 最小生成树-Prim算法和Kruskal算法 Prim算 ...

  6. 最小生成树Prim

    首先解释什么是最小生成树,最小生成树是指在一张图中找出一棵树,任意两点的距离已经是最短的了. 算法要点: 1.用book数组存放访问过的节点. 2.用dis数组保存对应下标的点到树的最近距离,这里要注 ...

  7. 最小生成树Prim算法和Kruskal算法

    Prim算法(使用visited数组实现) Prim算法求最小生成树的时候和边数无关,和顶点树有关,所以适合求解稠密网的最小生成树. Prim算法的步骤包括: 1. 将一个图分为两部分,一部分归为点集 ...

  8. 最小生成树 Prim Kruskal

    layout: post title: 最小生成树 Prim Kruskal date: 2017-04-29 tag: 数据结构和算法 --- 目录 TOC {:toc} 最小生成树Minimum ...

  9. poj1861 最小生成树 prim &amp; kruskal

    // poj1861 最小生成树 prim & kruskal // // 一个水题,为的仅仅是回味一下模板.日后好有个照顾不是 #include <cstdio> #includ ...

随机推荐

  1. Xshell6连接虚拟机(一)

    普通用户转换成管理员: 一.首先进入终端: 1.输入:    su   然后回车 2.若输入密码 则显示认证失败,说明还是普通管理员身份. (1)设置新密码:        sudo passwd r ...

  2. 测试理论- the conten of test plan

    1 testing objects 测试对象 2 testing scope 测试范围 3 testing the frame (?) 4 the environment 5 reason for t ...

  3. Coursera: Internet History, Technology, and Security

    课程网址:https://www.coursera.org/learn/internet-history 学习笔记: Week 1: History - Dawn of Early Computing ...

  4. POI实现excel的数据验证

    目录 前言 难点1:合并单元格 代码实现策略: step 1: 合并单元格 step 2: 给单元格赋值 难点2:数据验证-下拉框 代码实现策略: step 1:设置需要进行数据验证的单元格范围和可供 ...

  5. Android Studio的初体验

    在机缘巧合之下遇到了安卓开发,接触了Android Studio开始了漫长的改bug的道路,以下为简易版心酸历程 首先我需要成功安装Android Studio,由于我过于叛逆以及为了避免出错于是从一 ...

  6. System.NullReferenceException:未将对象引用设置到对象的实例,这是一个新鸟,中鸟,老鸟都避不开的错误

    原文链接:http://www.jb51.net/article/30005.htm

  7. To Chromium之版本管理

    Git. 1.由于想直接submit到Chromium的官方Branch需要申请权限,目前拿不到,所以打算snapshot一个chromium版本. 本地搭建一个git的server/client,方 ...

  8. WebService使用介绍(二)

    Soap soap是什么 SOAP 是一种网络通信协议 SOAP即Simple Object Access Protocol简易对象访问协议 SOAP 用于跨平台应用程序之间的通信 SOAP 被设计用 ...

  9. 第一个贴上XMT标签的Hadoop程序

    距离老板留给我并行化做属性约简的任务开始到今天,已是一周有余,期间经历过各种呕心沥血,通宵达旦,终于运行出了一个结果.其中在配置过程中,浪费了爷大量的时间,有时回想自己上个周干的事情,会觉得分明的本末 ...

  10. Codeforces 821E Okabe and El Psy Kongroo(矩阵快速幂)

    E. Okabe and El Psy Kongroo time limit per test 2 seconds memory limit per test 256 megabytes input ...