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

PP触发EWM产生PMR捡配

PP触发EWM产生PMR捡配 前言 PMR: Production Material Request 在流程订单/生产订单下达后,可以在EWM中产生PMR,在EWM中, PMR其实代表了ERP中的流程订单/生产订单。 You can use SAP EWM in conjunction with the following PP application components: • Production Orders • Process Orders • Repetitive Manufacturing •

ZevYang的博客 1930

camera 知识速查

本文总结了Camera开发中的常见问题排查方法和关键技术点。主要内容包括:I2C/MIPI不通的排查步骤、时序问题分析方法、Sensor/AF/Flash/PDAF的Bring Up流程、OTP调试方法、PDAF原理及调试、CTS/ITS问题解决思路、AE震荡解决方案、Systrace性能分析方法、Flicker产生原理、Tombstone问题定位等。还涉及V4L2框架解析、问题定位分层方法、Sensor Setting注意事项、时钟信号概念、第三方应用卡顿排查、大光圈概念、Monkey测试方法。

qq_41665131的博客 787

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

用STM32L552驱动AD9102:一个硬件工程师的实战避坑指南(附完整代码)

本文详细介绍了如何使用STM32L552微控制器驱动AD9102任意波形发生器芯片,从硬件设计到波形生成的完整实战指南。内容包括电源架构、时钟电路设计、输出调理电路等关键硬件设计要点,以及软件驱动架构、波形存储优化和典型波形生成实例。通过实战经验分享,帮助硬件工程师避开常见设计陷阱,提升开发效率。

weixin_30889885的博客 337

最小生成树

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

【2025年五一数学建模竞赛】A题 完整论文 模型建立与求解

2025年五一数学建模竞赛 A题:基于历史数据与模式识别的道路车流量推测模型研究

Magnolia_He的博客 3792

深度学习二分类实战:用Python+Keras搞定基因表达数据预测(附完整代码)

本文详细介绍了如何使用Python和Keras构建深度学习模型进行基因表达数据的二分类预测。通过数据预处理、特征选择、神经网络构建及优化策略,特别针对小样本场景提供了调参技巧和模型解释方法。文章还包含完整的代码示例,帮助读者快速掌握深度学习在生物信息学中的应用。

weixin_30642561的博客 251

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

SAP财务模块必看:BAPI_ACC_DOCUMENT_POST扩展字段全攻略(含FIBF配置)

本文详细解析了SAP财务模块中如何利用BAPI_ACC_DOCUMENT_POST的EXTENSION1参数扩展自定义字段。文章从需求分析入手,对比了传统增强与BTE(业务交易事件)两种技术路径,并重点提供了基于FIBF配置的完整实战指南,包括数据结构理解、函数模块开发及配置步骤,帮助ABAP开发者和财务顾问高效、可维护地实现如WBS元素、税务代码等字段的灵活传递。

weixin_29290963的博客 404

(最小生成树)POJ1258Agri-Net

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

Z7784562的博客 395

基于R语言的PLS算法实现解读

我们使用"pls"包来执行PLS回归,并提供了相应的源代码。在本文中,我们将详细解读基于R语言的PLS算法的实现过程,并提供相应的源代码。PLS算法的实现可以借助R语言中的"pls"包。该包提供了丰富的函数和工具,用于执行PLS回归、PLS-DA(PLS-Discriminant Analysis)和其他相关任务。除了执行PLS回归,"pls"包还提供了其他功能,例如PLS-DA(用于分类问题)和PLS变量选择等。上述代码将基于输入的X值,使用训练好的PLS模型进行预测,并返回预测结果。

TechWhizKid的博客 1057

Python 爬虫实战:科研数据库学术论文数据抓取

在科研工作中,学术论文是获取前沿知识和研究方法的重要资源。然而,手动从科研数据库中查找和整理论文信息往往耗时费力。幸运的是,Python 爬虫技术可以自动化这一过程,帮助我们高效地抓取和管理学术论文数据。本文将详细介绍如何使用 Python 爬虫技术抓取科研数据库中的学术论文数据,并进行全字段提取和文献管理,助力科研工作。

u014481728的博客 958

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

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

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

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

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

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

woshinannan的专栏 363

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

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

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、付费专栏及课程。

余额充值