Agri-Net POJ - 1258 (最小生成树)

POJ1258 Agri-Net中英对照翻译与参考解答 POJ1258 Agri-Net 传送门: POJ1258 关键词: 图;Prim 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... 阅读详情
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

code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int G[105][105];
int n;
struct node{
    int from,to;
    int len;
}farm[10005];
bool cmp(node a,node b){
    return a.len < b.len;
}
int pre[105];
int Find(int x){
    if(x == pre[x])
        return x;
    else return pre[x] = Find(pre[x]);
}
int Union(int x,int y){
    int fx = Find(x),fy = Find(y);
    if(fx == fy)
        return 0;
    else{
        pre[fy] = fx;
        return 1;
    }
}
int main(){
    while(~scanf("%d",&n)){
        int i,j;
        for(i = 0; i < 105; i++)
            pre[i] = i;
        int cnt = 0;
        for(i = 0; i < n; i++){
            for(j = 0; j < n; j++){
                scanf("%d",&G[i][j]);
                if(G[i][j] != 0){
                    farm[cnt].from = i;
                    farm[cnt].to = j;
                    farm[cnt++].len = G[i][j];
                }
            }
        }
        sort(farm,farm+cnt,cmp);
        int num = 0;
        int sum = 0;
        for(i = 0; i < cnt; i++){
            if(Union(farm[i].from,farm[i].to)){
                sum += farm[i].len;
                num++;
            }
            if(num == n-1)
                break;
        }
        cout << sum << endl;
    }
    return 0;
}

poj1258 Agri-Net 最小生成树 prim kruskal 模板 Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 56702   Accepted: 23513 Description Farmer John has been elected mayor of his town! One of his campa 阅读详情

相关推荐

数据结构 图的最小生成树 C++描述 使用prim算法、kruskal算法

分别利用prim算法和kruskal算法实现求图的最小生成树 C++描述

POJ_1258_Farmer John has been【基础最小生成树】

Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring in

持之以恒_fighting 3278

1258:Agri-Net(弗洛伊德)

1258:Agri-Net 弗洛伊德思路: 先找到与1相连最短的那条路,然后再更新与1和刚才相连的点每个与其他点最短的路存储,然后再找到下一个点,再找到这三个点与其他点最短路存储,以此类推。 Prim代码: 除了最后一个ac代码是自己的其他的都不是,wa的全是自己进步的过程,加油! #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define ll

《POJ 1258 - Agri-Net》的题解

《POJ 1258 - Max Sum Plus Plus》的题解 题目的描述 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...

weixin_46163391的博客 715

Odinub开源掌机:从硬件选型到软件定制,打造专属复古游戏体验

模拟器技术通过软件模拟经典游戏硬件的运行环境,其核心原理在于精确复制原硬件的CPU指令集、图形与音频处理逻辑。这项技术的价值在于以极低成本保存和重现数字文化遗产,让经典游戏跨越硬件生命周期得以延续。在工程实践中,开发者常基于Linux系统构建轻量级游戏环境,并利用RetroArch统一框架整合各类模拟器核心,实现跨平台游戏的集中管理与优化。具体到复古游戏掌机应用场景,需在性能、功耗与手感间精细平衡,例如选用RK3566等低功耗SoC,搭配Batocera等定制系统,最终自然收敛到Odinub这类开源项目——

weixin_30954817的博客 291

详解STM32F407双重ADC同步采集与DMA传输的配置技巧及实战应用

本文详细解析了STM32F407双重ADC同步采集与DMA传输的配置技巧及实战应用。通过CubeMX配置、代码实现和常见问题解决方案,帮助开发者高效实现多通道数据同步采集,提升工业自动化和嵌入式系统的数据采集效率与精度。

ii567的博客 368

Agri-Net (最小生成树)

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 spee...

WhileTrue 369

从零开始:在Ubuntu 22.04上配置Isaac Gym Preview 3环境(含常见错误解决方案)

本文提供了在Ubuntu 22.04系统上从零开始配置Isaac Gym Preview 3环境的完整实战指南。内容涵盖NVIDIA驱动安装、Conda环境搭建、CUDA与PyTorch版本精准匹配、Isaac Gym核心安装与验证,并重点介绍了针对四足机器人等复杂系统的强化学习训练环境配置、常见错误解决方案及性能调优技巧,旨在帮助开发者高效部署这一高性能物理仿真平台。

weixin_29048775的博客 399

最小生成树

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 spee

baoluqi的博客 450

Halcon VariableInspect (变量检查拓展)安装指南

网址2;

T 975

BP神经网络回归预测+SHAP可解释分析+新数据预测(MATLAB完整源码)

BP神经网络回归预测+SHAP可解释分析+新数据预测(MATLAB完整源码)

机器学习之心的博客,关注并私信文章链接,获取对应文章源码和数据。 398

prim算法 poj1258 Agri-Net 最小生成树基础题

Agri-NetTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 64460 Accepted: 26642DescriptionFarmer John has been elected mayor of his town! One of his campaign promises was to bring internet con...

bestsort 490

boost::circular_buffer缓行缓冲区

1、circular_buffer实现了一个环形缓冲区,空间大小是固定的,当到达容器末尾时,将自动利用容器另一端的位置空间。 2、当程序需要容纳大量的元素,但多数情况下仅保存较少数量元素时可以使用circular_buffer_space_optimized,他可以有效的减少内存的占用,提高程序的运行效率。 3、主要接口: 4、实现一个生产者---消费者模式示例: #inc...

小米的修行之路 799

(最小生成树)POJ1258Agri-Net

POJ1258Agri-Net 题意&思路: 给n个农场之间的距离,要铺设光缆使之两两连通,问最小铺设光缆的距离。 模板题。 代码: #include<iostream> #include<stdio.h> #include<stdlib.h> #include<algorithm> #include<string.h> #inc...

Z7784562的博客 395

教程: 将 SolidWorks 设计导出为 ROS 的 URDF

请按照以下步骤应用自定义徽标纹理:创建徽标材质文件:techniquepass材质名称:测试/材质颜色属性:环境、漫反射、镜面和发射都设置为白色(1 1 1 1),这意味着该材质看起来完全是白色的。纹理 在表面应用纹理图像 logo.png。将材质文件复制到 gazebo matrial:复制徽标图像:将 “徽标 ”替换为机器人上徽标所在位置的适当参考值。按照以上步骤,您就可以在 Gazebo 中为机器人添加自定义材质和纹理,包括徽标。

机器人控制与仿真应用 2464

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

POJ 1258 Agri-Net (prim 最小生成树)

题目链接: POJ 1258 Agri-Net (prim 最小生成树) 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 n

Dextrad_ihacker的博客 644

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

POJ1258 Agri-Net(最小生成树)

题目链接:http://poj.org/problem?id=1573 解题思路: 最小生成树!!! AC代码: #include #include #include #include using namespace std; const int maxn=100010; int pa[maxn]; int n,m,sum; struct Edge { int

piaocoder 567

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

poj 1258 Agri-Net 基本&经典最小生成树

传送门:poj 1258 Agri-Net题目大意n个农场,已知这n个农场都互相相通,有一定的距离,现在每个农场需要装光纤,问怎么安装光纤能将所有农场都连通起来,并且要使光纤距离最小,输入的是邻接矩阵,输出安装光纤的总距离解题思路裸prim, 1. 随便找一个元素进行寻找 2. 找出和这个元素相邻的所有权值,如果不能到达表示无限大 3. 找到和这个元素相邻的最小权值,记录下来 4. 最小权值

woshinannan的专栏 363

POJ 1258 Agri-Net 最小生成树

POJ 1258 Agri-Net 最小生成树  题意:有n个点,给你一个方阵代表他们两两之间的距离,求最小生成树。  思路:大水题,直接prim解决。AC代码:// // main.cpp // L // // Created by LucienShui on 2017/5/11. // Copyright © 2017年 LucienShui. All rights reserved.

LucienShui 586

poj1258 - Agri-Net (经典邻接矩阵 求最小生成树) (Prim & Kruskal)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 40028   Accepted: 16291 Description Farmer John has been elected mayor of his town! One of his campaig

ITCTO-疯男的CSDN博客 878

POJ 1258 Agri-Net(最小生成树)

POJ 1258 Agri-Net(最小生成树) http://poj.org/problem?id=1258 题意:        给你一个N顶点的无向图的距离邻接矩阵,问你使得整个图连通的最小边长和是多少? 分析:        最小生成树入门题,直接kruskal模板. AC代码: #include #include #include using namespace std

code 1033

最小生成树(Kruskal)POJ 1258 Agri-Net

最小生成树(Kruskal)POJ 1258 Agri-Net 题目链接: Agri-Net 题目基础: 最小生成树-Kruskal(克鲁斯卡尔)算法 思路: 题目大意: 有N个村庄,输入每个村庄到编号为1~N的距离,每个村落都需要网络覆盖,问网线的最短长度 题解: 最小生成树的裸题,按模板打 说个优化吧,样例给的是一个矩阵,但很容易想到,1到3的距离和...

[SZU_Crayon] 510

POJ 1258 Agri-Net 【最小生成树入门题目】

原题链接:http://poj.org/problem?id=1258 周四上了节数据结构终于知道何为最小生成树,晚上回寝,遂得此题! Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29613   Accepted: 11750 Des

free斩 5513

POJ-1258 Agri-Net(最小生成树+Prim算法)

POJ-1258 Agri-Net(最小生成树+Prim算法) Agri-NetTime Limit: 1000MS Memory Limit: 10000K   DescriptionFarmer John has been elected mayor of his town! One of his campaign promises was to bring internet connecti...

碳酸钙的01妖精的博客 252

POJ 1258 Agri-Net [最小生成树] 《挑战程序设计竞赛》2.5

POJ 1258 Agri-Net 求最小生成树

yoer77的博客 354
上一篇: Six Degrees of Cowvin Bacon POJ - 2139 (floyd求最短路)
下一篇: Candies POJ - 3159 (差分约束+最短路)
Guuuuuu老师儿
博客等级 码龄9年 259粉丝 718原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址