当前位置: 首页 > news >正文

acwing算法提高之图论--最小生成树的典型应用

目录

  • 1 介绍
  • 2 训练

1 介绍

本专题用来记录使用prim算法或kruskal算法求解的题目。

2 训练

题目1:1140最短网络

C++代码如下,

#include <iostream>
#include <cstring>using namespace std;const int N = 110, INF = 0x3f3f3f3f;
int g[N][N];
int d[N];
bool st[N];
int n, m;void prim() {memset(d, 0x3f, sizeof d);int res = 0;for (int i = 0; i < n; ++i) {int t = -1;for (int j = 1; j <= n; ++j) {if (!st[j] && (t == -1 || d[t] > d[j])) {t = j;}}st[t] = true;if (i) res += d[t];for (int j = 1; j <= n; ++j) {if (d[j] > g[t][j]) {d[j] = g[t][j];}}}cout << res << endl;return;
}int main() {cin >> n;for (int i = 1; i <= n; ++i) {for (int j = 1; j <= n; ++j) {cin >> g[i][j];}}prim();return 0;
}

题目2:1141局域网

C++代码如下,

#include <iostream>
#include <cstring>
#include <algorithm>using namespace std;const int N = 110, M = 210;
int p[N];
int n, m;struct Edge {int a, b, w;bool operator< (const Edge &W) const {return w < W.w;}
}edges[M];int find(int x) {if (p[x] != x) p[x] = find(p[x]);return p[x];
}int main() {cin >> n >> m;int s = 0;for (int i = 0; i < m; ++i) {cin >> edges[i].a >> edges[i].b >> edges[i].w;s += edges[i].w;}for (int i = 1; i <= n; ++i) p[i] = i;sort(edges, edges + m);int res = 0, cnt = 0;for (int i = 0; i < m; ++i) {int a = edges[i].a, b = edges[i].b, w = edges[i].w;a = find(a);b = find(b);if (a != b) {p[a] = b;res += w;cnt++;}}cout << s - res << endl;return 0;
}

题目3:1142繁忙的都市

C++代码如下,

#include <iostream>
#include <cstring>
#include <algorithm>using namespace std;const int N = 310, M = 8010;
int n, m;
int p[N];struct Edge {int a, b, w;bool operator< (const Edge &W) const {return w < W.w;}
}edges[M];int find(int x) {if (p[x] != x) p[x] = find(p[x]);return p[x];
}int main() {cin >> n >> m;for (int i = 1; i <= n; ++i) p[i] = i;for (int i = 0; i < m; ++i) {cin >> edges[i].a >> edges[i].b >> edges[i].w;}sort(edges, edges + m);int res = 0;int cnt = 0;for (int i = 0; i < m; ++i) {int a = edges[i].a, b = edges[i].b, w = edges[i].w;a = find(a);b = find(b);if (a != b) {p[a] = b;cnt += 1;res = w;}}cout << cnt << " " << res << endl;return 0;
}

题目4

http://www.lryc.cn/news/330522.html

相关文章:

  • springcloud基本使用二(远程调用)
  • 代码随想录刷题day42| 01背包理论基础分割等和子集
  • Python文件操作命令
  • CSS面试题---基础
  • OpenHarmony实战开发-分布式数据管理
  • 微服务(基础篇-007-RabbitMQ部署指南)
  • C语言一维数组及二维数组详解
  • 11.图像边缘检测的原理与实现
  • RVM安装ruby笔记
  • 电力系统负荷预测方法
  • electron打包桌面版.exe之vue项目踩坑(vue3+electron 解决打包后首页打开空白,打包后路由不跳转及请求不到后端数据等问题)
  • MySQL学习笔记(持续更行ing)
  • 服务器配置Huggingface并git clone模型和文件
  • Rust 开发的高性能 HTTP 请求工具
  • Android Studio 通过 WIFI 调试手机 app
  • RabbitMQ高级笔记
  • 【Qt】QtCreator交叉编译环境配置Qt mkspec
  • 点点数据K参数加密逆向分析(RPC方案跟加密算法还原)
  • 考研数学|《1800》+《660》精华搭配混合用(经验分享)
  • 【Redis 二】Redis客户端(Jedis、SpringDataRedis、RedisTemplate)
  • Java中Filter和Interceptor的区别
  • 记一次 pdfplumber 内存泄漏导致的服务器宕机
  • SpringBoot单元测试剖析
  • 【华为OD机试C++】计算某字符出现次数
  • ORA-01779 BYPASS_UJVC 11.2后废弃了
  • 验证码demo(简单实现)
  • C#面:虚函数和抽象函数的区别
  • Vidmore Video Fix for Mac 视频修复工具
  • Docker容器与虚拟化技术:OpenEuler 部署 Docker UI
  • 328——二维矩阵值变为1最小操作次数 next、nextInt、nextLine