poj-1258-Agri-Net

【代码超详解】POJ 1258 Agri-Net(Kruskal 算法 · 模板) 一、题目描述 二、算法分析说明与代码编写指导 三、AC 代码 #include<cstdio> #include<algorithm> #pragma warning(disable:4996) using namespace std; template<size_t n> class union_find { private: unsigned roo... 阅读详情

 

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

 

 

题意:说白了就是求最小生成树。

中考考砸了好不爽_(:з」∠)_

 

看到数据范围和输入格式,我就想,干脆用prim吧,不想转成边然后排序,反正也不会TLE——能AC就行。

(虽然我一个多月没写过这些了可能这次写丑了)

 

/////////////////////////何谓prim//////////////////////

就是最开始选一个点到最小生成树中,然后每次添加一个离树的距离最短的点。

////////////////////////简介完毕///////////////////////

 

还有一个事情,这道题有多组数据!多组数据!多组数据!!该清零的清零!(可是我并不是WA在这里呢)

我之前压根没发现这道题有多组数据..........

 

 

 

下为代码。

 

#include<cstdio>
#include<cstring>
int a[105][105],n,itr[105],ans;
bool ir[105];
int main()
{
	int i,j,k,minn,c;
	while(scanf("%d",&n)==1)
	{
		memset(ir,0,sizeof(ir));
		ir[1]=true;ans=0;itr[1]=1;
		for(i=1;i<=n;i++)
			for(j=1;j<=n;j++)
				scanf("%d",&a[i][j]);
		for(i=2;i<=n;i++)
		{
			minn=100001;
			for(j=1;j<i;j++)
				for(k=1;k<=n;k++)
					if(!ir[k]&&minn>a[itr[j]][k])
					{
						minn=a[itr[j]][k];c=k;
					}
			itr[i]=c;ir[c]=1;ans+=minn;
		}
		printf("%d\n",ans);
	}
}

 

 

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 立即下载

相关推荐

视频监控设备(NVR、IPC等)如何通过国标GB28181接入到视频监控平台,以及接入过程如何测试视频接入是否成功

GB/T28181标准广泛的应用,视频监控设备通过国标GB28181接入视频监控平台的过程变成程序化、标准化的互联过程,确保了视频监控设备能够在不同系统、不同网络环境下实现远程、跨区域、跨级别的访问和控制。版本有GB28181-2011、2016、2022几个常用版本,威迪斯特的视频监控平台完全支持GB28181的这几个版本,能够实现不同品牌视频监控设备的国标视频接入(国标视频监控接入),或者为国标28181视频接入(国标28181视频监控接入),也为国标281818接入(国标GB/T28181接入。

weixin_70208651的博客 1万+

Agri-Net POJ 1258

农夫约翰被选为他的镇长!他的竞选承诺之一是将互联网连接到该地区的所有农场。他当然需要你的帮助。 农民约翰为他的农场订购了高速连接,并将与其他农民共享他的连接。为了最大限度地降低成本,他希望铺设最少量的光纤,将他的农场与所有其他农场连接起来。 给出连接每对服务器场需要多少光纤的列表,您必须找到将它们全部连接在一起所需的最小光纤数量。每个场必须连接到其他某个场,这样数据包才能从任何一个场流向任何其他场...

KEIke0的博客 562

Understand-Anything:AI驱动的代码知识图谱,实现项目全局洞察与高效导航

在软件开发中,理解复杂代码库的结构和依赖关系是提升开发效率的关键。传统代码阅读方式往往陷入局部视角,难以快速把握系统全貌。知识图谱技术通过将代码实体(如文件、类、函数)及其关系(调用、继承、依赖)可视化,为代码理解提供了全局视角。这项技术的核心价值在于将静态代码转化为动态、可交互的认知地图,极大降低了系统学习与维护成本。在实际工程中,它广泛应用于新成员快速入职、遗留系统梳理、架构影响分析等场景。本文介绍的Understand-Anything工具,正是基于这一理念,通过多智能体分析管道,自动构建代码的结构图

weixin_42615290的博客 883

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

有n个农场,已知这n个农场都互相相通,有一定的距离,现在每个农场需要装光纤,问怎么安装光纤能将所有农场都连通起来,并且要使光纤距离最小,输出安装光纤的总距离 任意两个村庄之间的距离小于 100,000. Input 输入包含多组数据。对于每组数据, 第一行包含一个整数N表示农场的数量 (3 &lt;= N &lt;= 100). 接下来是一个NXN的邻接矩阵,表示各个村庄之间的距离 . 当然...

博客 255

VMware桥接模式实战:用手机热点给Linux虚拟机共享网络(附WiFi适配技巧)

本文详细介绍了在移动办公场景下,如何为VMware中的Linux虚拟机配置桥接模式,以通过手机热点共享网络。文章深入解析了桥接模式在无线环境下的工作原理,提供了从主机端准备、VMware设置到Linux虚拟机内部配置的完整实战步骤,并针对手机热点的特殊性,分享了MTU优化、DNS配置及常见网络问题的诊断与解决方案,帮助用户实现稳定高效的移动虚拟化办公。

2y3u4i5o6p的博客 1196

中国裁判文书网案例批量爬取

本文介绍了某法院网站数据采集的技术研究过程,主要包含JS逆向分析和数据解密方法。通过开发者工具分析发现页面数据经过加密处理,重点研究了ciphertext等加密参数的生成逻辑。文章详细记录了定位加密函数、扣取加密代码、解密数据处理的全流程,并验证了解密方法的有效性。研究过程中强调了技术使用的法律边界,明确声明仅用于合法技术研究,禁止任何非法数据爬取行为。最终实现了加密数据的成功解密,并解释了各请求参数的作用,为后续数据清洗和存储奠定了基础。

2303_80825459的博客 817

poj-1258(Agri-Net)【图的最小生成树】

自学的数据结构,中午看到了图论的最小生成树,想着找个题巩固一下,找到了这个,结果一直错一直错,开始怀疑这本书的算法是不是有问题。。。,郁闷了一下午,晚上突然发现codevs上有这个题目,用了点办法截了一组错误数据,终于发现了错在哪,真是粗心呃。。。。。 #include <iostream> #include <cstdio> #define MAX 0x3f3...

weixin_33811539的博客 203

brother的PT-9800PCN标签打印机二次开发

本来想通过esc/p指令打印标签,但是无奈能找到的资料太少,而且打电话给客服,也没有sample可以提供参考,后来客服说使用template功能的话,打印很方便。于是下载手册学习了一下, 先下载P-touch Editor软件,制作标签模板。 制作的模板就是普通的标签,不需要连接数据库之类的,模板里有3个object,分别是文本1,文本2,条形码3。这个很关键,后面的代码传送数据,就是按这...

3563

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

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

black_hole6的博客 426

解决【x2openEuler环境检查未通过——CheckRepoCurlActor不通过】问题

摘要:x2openEuler使用默认repo源时出现环境检查失败问题,原因是默认源返回308永久重定向错误。解决方法包括:1)为CentOS主机配置华为yum源;2)在x2openEuler Web界面修改为自定义可用源。具体步骤包括备份原源、下载华为源、清除缓存,并通过curl测试确认新源可用性。最终成功配置openEuler-24.03-LTS-SP1版本的repo源后,环境检查顺利通过。

提供C#相关开发 、云计算、运维测试、网络安全相关技术分享与服务,有意向可联系。 258

SSE 传输过程中,丢失空格和回车符,怎么解决?

Server - Sent Events(SSE)即服务器发送事件,是 HTML5 中的一项 Web API,用于实现服务器向客户端浏览器推送实时数据的功能。

934

POJ 1258 Agri-Net

POJ 1258 Agri-Net 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. F...

旺崽的博客 2942

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

《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

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

POJ-1258 Agri-Net(模板prim算法)

POJ-1258 Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 81114 Accepted: 33478 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

00后的博客 287

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

【MST-Prime】POJ 1258 Agri-Net

POJ 1258 Agri-Net MST模板题 Prime算法:跑点,维护点到树上的最短距离,跟最短路有本质区别。 #include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include <cmath> #include <cstri...

L_X_ 229

poj 1258 Agri-Net

poj 1258 Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 51094 Accepted: 21275 DescriptionFarmer John has been elected mayor of his town! One of his campaign promises

Switch的博客 1118

poj 1258-Agri-net

//poj 1258-Agri-net/* * 图论-最小生成树 * prim+priority_queue(邻接矩阵) 实现 * !!!有多组数据 * wa wa wa ac 248k 0ms */#include #include #include #include #include #include #include

gold_bridge 1002

(最小生成树)POJ1258Agri-Net

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

Z7784562的博客 395

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

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 488

poj 1258 Agri-Net

poj 1258 Agri-Net 这里,本来是用prim算法更好一点,但是,emmm,确实还没有掌握该算法的具体实现步骤,所以先使用了Kruskal算法进行求解,对于这类图的问题,需要先转化成两点之间线路的形式进行存储后再使用Kruskal算法进行求解。 #include <iostream> #include <string.h> #include <algor...

CUCUC1的博客 172

Agri-Net_POJ1258

题目 链接 POJ1258 Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 56713   Accepted: 23518 Description Farmer John has been elected mayor of his to

MLee 的博客 442

poj1258 Agri-Net

poj1258 Agri-Net

李典金 215
上一篇: poj-2393-Yogurt Factory
下一篇: 数位dp
dogeding
博客等级 码龄10年 31粉丝 109原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值