Agri-Net
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 44670   Accepted: 18268

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
求把所有town连接起来所需最少边权值和,把边权值按从小到大排序,如果当前边权值连接的两边没有关系就把当前边权值相加,如果有关系当前值就没有价值,不用浪费资源联通这两个town。到最后肯定会把所有town连接~
#include<iostream>
#include<algorithm> using namespace std; #define N 1000005 int k, n, a, f[N]; struct node
{
int x, y, w;
}P[N]; int cmp(const void *u, const void *b)
{
node c, d;
c = *(node *)u;
d = *(node *)b; return c.w-d.w;
} void init()
{
for(int i = ; i < N; i++)
f[i] = i;
}
int found(int a)
{
if(f[a] != a)
f[a] = found(f[a]);
return f[a];
} int main()
{
while(cin >> n)
{
k = ; init(); for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
{
cin >> a;
P[k].w = a;
P[k].x = i, P[k].y = j;
k++;
} qsort(P, k, sizeof(P[]), cmp); int ans = ; for(int i = ; i < k; i++)
{
int nx = found(P[i].x), ny = found(P[i].y); if(nx != ny)
{
f[nx] = ny;
ans += P[i].w;
}
}
//int ans = kuscral(1, n); cout << ans << endl;
}
return ;
}

Agri-Net —poj1258的更多相关文章

  1. Agri Net POJ1258 && Constructing Roads POJ2421

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

  2. A过的题目

    1.TreeMap和TreeSet类:A - Language of FatMouse ZOJ1109B - For Fans of Statistics URAL 1613 C - Hardwood ...

  3. poj1258 Agri-Net (prim+heap)

    题目链接:poj1258 Agri-Net 这题我上个月做过,是个大水题,今天看见有人用prim+heap做的,就学习了下. #include<cstdio> #include<cs ...

  4. POJ-1258 Agri-Net---MST裸题Prim

    题目链接: https://vjudge.net/problem/POJ-1258 题目大意: 求MST 思路: 由于给的是邻接矩阵,直接prim算法 #include<iostream> ...

  5. Poj1258 Agri-Net (最小生成树 Prim算法 模板题)

    题目链接:http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One ...

  6. poj1258 Agri-Net(Prime || Kruskal)

    题目链接 http://poj.org/problem?id=1258 题意 有n个农场,现在要在n个农场之间铺设光纤使得n个农场连接起来,求铺设光纤的最短距离. 思路 最小生成树问题,使用Prime ...

  7. 最小生成树 prime poj1258

    题意:给你一个矩阵M[i][j]表示i到j的距离 求最小生成树 思路:裸最小生成树 prime就可以了 最小生成树专题 AC代码: #include "iostream" #inc ...

  8. poj1258 Agri-Net 最小生成树

    Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44032   Accepted: 18001 Descri ...

  9. Prim算法POJ1258

    http://poj.org/problem?id=1258 这道题是最简单的一个啦,,,, #include<stdio.h> #include<iostream> #inc ...

  10. POJ1258 基础最小生成树

    本文出自:http://blog.csdn.net/svitter 题意:给出一个数字n代表邻接矩阵的大小,随后给出邻接矩阵的值.输出最小生成树的权值. 题解: prime算法的基本解法: 1.选择一 ...

随机推荐

  1. 第 13 章 python并发编程之io模型

    一.IO模型介绍 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞(non-blocking)IO分别是什么,到底有什么区别?这个问 ...

  2. 【BASIS系列】SAP 设置系统timeout时间

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[BASIS系列]SAP 设置系统timeout ...

  3. spring事务——try{...}catch{...}中事务不回滚的几种处理方式(转载)

    转载自   spring事务——try{...}catch{...}中事务不回滚的几种处理方式   当希望在某个方法中添加事务时,我们常常在方法头上添加@Transactional注解 @Respon ...

  4. spring boot 控制 序列化格式

    @PostMapping(path = "/register", produces={MediaType.APPLICATION_JSON_VALUE, MediaType.TEX ...

  5. struts框架的一些注意点

    1.Struts.xml文件中<include file="">标签的运用 用法:此标签引用配置文件,Struts2提供了一个默认的struts.xml文件,当此配置文 ...

  6. java_第一年_JavaWeb(9)

    JavaBean是一个遵循某种特定写法的Java类,有以下特点: 必需具有一个无参的构造函数 属性必需私有化 私有化的属性必需通过public类型的方法暴露给其它程序,其方法命名也有一定的规范 范例: ...

  7. 高级定时器-setTimeout()、setInterval()、链式setTimeout()

    使用 setTimeout()和 setInterval()创建的定时器可以用于实现有趣且有用的功能.执行时机是不能保证的,因为在页面的生命周期中,不同时间可能有其他代码在控制 JavaScript ...

  8. XMPP即时通讯协议使用(三)——订阅发布、断开重连与Ping

    package com.testV3; import java.util.List; import org.jivesoftware.smack.ConnectionListener; import ...

  9. jquery+html实现前端的上传图片预览

        就是这样的一个功能,点击加号,出现图片选择,然后选择好以后生成预览. input那么丑,UI看不惯,一定要改成加号 我就用了fa的图标,外部套一个bootstrap4中的class:borde ...

  10. 12-低延迟、全接口(HMDI、DVI、YPb Pr、RGB)H.264全高清编码器解码器

    低延迟.全接口(HMDI.DVI.YPb Pr.RGB)H.264全高清编码器解码器 一.产品介绍  1.近零延时的H.264压缩到1920x1080p60  该产品提供分辨率为1920x1080p6 ...