[poj1258 Agri-Net]最小生成树

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 阅读详情

复习了一下Kruskal和prim。

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

const int MAX = 128;
struct edge {
	int from, to;
	int cost;
	bool operator<(const edge& B)const {
		return cost < B.cost;
	}
};
edge G[MAX*MAX];
int a[MAX][MAX];
int father[MAX];

int find(int x) {
	if (father[x] == x) return x;
	return father[x] = find(father[x]);
}

bool Union(int x, int y) {
	int fx = find(x), fy = find(y);
	if (fx != fy) {
		father[fx] = fy;
		return true;
	}
	return false;
}

//m:边数 
int kruskal(int m) {
	sort(G + 1, G + m + 1);
	int s = 0;
	for (int i = 1; i <= m; ++i) {
		edge e = G[i];
		if (Union(e.from, e.to)) {
			s += e.cost;
		}
	}
	return s;
}

int read() {
	char ch;
	while ((ch = getchar()) < '0' || ch > '9');
	int x = ch - '0';
	while ((ch = getchar()) >= '0' && ch <= '9') {
		x = (x << 3) + (x << 1) + ch - '0';
	}
	return x;
}

int main() {
	int n;
	while (~scanf(" %d", &n)) {
		for (int i = 1; i <= n; ++i) {
			for (int j = 1; j <= n; ++j) {
				a[i][j] = read();
			}
		}
		int es = 0;
		for (int i = 1; i <= n; ++i) {
			for (int j = i + 1; j <= n; ++j) {
				if (a[i][j] > 0) {
					G[++es] = (struct edge){i, j, a[i][j]};
				}
			}
		}
		for (int i = 1; i <= n; ++i) father[i] = i;
		printf("%d\n", kruskal(es));
	}
	return 0;
}

下面是Prim算法的

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

const int MAX = 105;
int G[MAX][MAX];
int mincost[MAX];
bool vis[MAX];

int prim(int node) {
	memset(vis, false, sizeof(vis));
	fill(mincost, mincost + node + 1, 0xffffff);
	int sum = 0;
	mincost[1] = 0;
	for (int i = 1; i <= node; ++i) {
		int v = 0;
		for (int u = 1; u <= node; ++u) {
			if (!vis[u] && (v == 0 || mincost[v] > mincost[u])) {
				v = u;
			}
		}
		if (!v) break;
		sum += mincost[v];
		vis[v] = true;
		for (int i = 1; i <= node; ++i) {
			if (mincost[i] > G[v][i]) mincost[i] = G[v][i];
		}
	}
	return sum;
}
int read() {
	char ch;
	while ((ch = getchar()) < '0' || ch > '9');
	int x = ch - '0';
	while ((ch = getchar()) >= '0' && ch <= '9') {
		x = (x << 3) + (x << 1) + ch - '0';
	}
	return x;
}

int main() {
	int n;
	while (~scanf(" %d", &n)) {
		for (int i = 1; i <= n; ++i) {
			for (int j = 1; j <= n; ++j) {
				G[i][j] = read(); //scanf(" %d", &G[i][j]);
			}
		}
		printf("%d\n", prim(n));
	}
	return 0;
}



保姆级教程:在Zynq UltraScale+ EV系列FPGA上,用VCU IP核搞定4K60帧H265视频解码与HDMI2.0输出 本文提供在Zynq UltraScale+ EV系列FPGA上使用VCU IP核实现4K60帧H265视频解码与HDMI2.0输出的全流程指南。从开发环境准备、Vivado工程配置到PetaLinux系统定制,详细解析硬件选型、IP核参数设置及性能优化技巧,帮助开发者快速构建高效视频处理系统。 阅读详情

相关推荐

javaFx + SpringBoot 启动时报错:No auto configuration classes found in META-INF/spring.factories 解决方法

javaFx+SpringBoot+(de.roskenet:springboot-javafx-support:2.1.6)开发。 直接在idea中运行正常,但打包成jar,之后使用java -jar xxx.jar启动报错,发现日志中有: java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories. 以及 at de.felixroske.jfxsuppo

long117long的专栏 4611

(最小生成树)POJ1258Agri-Net

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

Z7784562的博客 395

图像去模糊(二)——Richardson–Lucy算法

Richardson Lucy Algorithm(Richardson-Lucy算法详解及效果实例)

xueruhongchen的博客 3万+

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 486

uniapp(全端兼容) - 最新详细实现接入腾讯视频播放器功能插件,uniapp全平台端(小程序/H5网页/App/nvu)调用引入并使用腾讯视频播放组件完整全流程(详细示例源码,一键复制开箱即用)

uniapp腾讯视频,uniapp微信小程序腾讯视频,uniapp小程序接入腾讯视频详细示例教程源码,uniapp怎么在微信小程序平台使用腾讯视频插件,在uni-app中使用腾讯视频插件播放视频,uniapp调用腾讯视频,uniapp引入腾讯视频插件,uniapp小程序中引入腾讯视频无法播放,uniapp微信小程序平台引入腾讯视频播放不了黑屏,在uniapp开发的微信小程序中使用腾讯视频,uniapp如何引入腾讯视频插件播放视频,uni-app小程序平台引入腾讯视频插件播放腾讯视频,uniapp 小程序接入

✅ 精选推荐文章 1万+

PX4从放弃到精通(二十八):垂起过渡控制

固件版本:1.14.0可加名片交流学习。

超维空间科技的博客 2873

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

2026必学:LangChain+Playwright爬虫,赋能RAG智能问答系统

RAG(检索增强生成)智能问答系统的效果,80%取决于数据源的质量。2026年,LangChain与Playwright的组合成为企业级RAG数据采集的黄金搭档:LangChain提供标准化的RAG数据处理链路,Playwright解决动态网页渲染的核心痛点,两者结合能高效采集、结构化处理各类网页数据(动态渲染页、复杂交互页、反爬页),为RAG系统提供高质量语料。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 1705

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 566

Spring AI 实战(11):收官!10 道面试高频题 + 一张脑图带走全系列

本文是Spring AI系列的收官篇,通过知识脑图梳理前10篇内容,提供面试高频题和避坑清单。主要内容包括: 知识脑图:分6层(核心抽象、输入增强、输出控制等)总结Spring AI全栈技术。 10道高频面试题:涵盖ChatClient/ChatModel关系、结构化输出、RAG链路等核心知识点。 避坑清单:列出9条常见问题及解决方案,如向量维度匹配、Advisor顺序等。 技术对比:分析Spring AI、LangChain4j和Spring AI Alibaba的差异与适用场景。 使用建议:提供面试复习

不才不才不不才的博客 296

Android 13 内置xapk安装包的方法

安装xapk 内置xapk

安卓兼职framework和app工程师的博客 223

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

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

woshinannan的专栏 362

PCL Alpha Shapes平面点云边界特征提取【2026最新版】

alpha shapes算法用于平面点云的边界特征提取。博客长期更新,本文最近一次更新时间为:2026年4月20日。

点云侠的博客 27万+

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 585

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

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

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

code 1031

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

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-疯男的优快云博客 878

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

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

[SZU_Crayon] 509

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斩 5512

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

POJ 1258 Agri-Net 求最小生成树

yoer77的博客 353

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(最小生成树 Kruskal)

题意  给你农场的邻接矩阵  求连通所有农场的最小消耗 和上一题一样裸的最小生成树 #include #include #include using namespace std; const int N = 105, M = 10050; int par[N], ans, n, m, t; struct edge { int u, v, w;} e[M]; bool cmp(edge a,

Deng's site \# 1533

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

20-1-16-Prime-POJ1258-Minimun_Spanning_Tree

POJ1258 Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 77439 Accepted: 31957 Description Farmer John has been elected mayor of his town! One of his campaign promises...

shen253135371的博客 201

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

poj 1258 Agri-Net dijkstra迪杰斯特拉算法,最短路径问题的变形,最小生成树

用最少的路线,把所有的农场连接起来。 涉及到最小生成树,但我的代码只用了他的思想 思路:从第一个点开始走,用一个数组vis[N]来记录农场是否已连接。 map[N][N]矩阵存储关系。 主要的是在已访问的点与未访问的点之间

雪的期许 851

poj1258Agri-Net(最小生成树)

题目链接:http://poj.org/problem?id=1258 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 need...

awli5131的博客 267

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

旺崽的博客 2941
上一篇: [poj2349 Arctic Network]最小生成树
下一篇: 【最长上升子序列LIS】O(n^2)和O(nlogn)算法简记
bit_line
博客等级 码龄12年 33粉丝 150原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值