当前位置:网站首页>LeetCode 1584. 连接所有点的最小费用
LeetCode 1584. 连接所有点的最小费用
2022-07-13 19:04:00 【HumbleFool】
LeetCode 1584. 连接所有点的最小费用
Kruskal 更加适合稀疏图
const int N = 5e5 + 10;
struct Edge
{
int a, b, w;
bool operator < (Edge& e)
{
return w < e.w;
}
}e[N];
class Solution {
public:
int p[N];
int find(int x)
{
if(x != p[x]) p[x] = find(p[x]);
return p[x];
}
int kruskal(int n)
{
sort(e, e + n);
int res = 0, cnt = 0;
for(int i = 0; i < n; i ++)
{
int a = e[i].a, b = e[i].b, w = e[i].w;
int pa = find(a), pb = find(b);
if(pa != pb)
{
p[pa] = pb;
cnt ++;
res += w;
}
if(cnt == n - 1) break;
}
return res;
}
int minCostConnectPoints(vector<vector<int>>& points) {
int n = points.size();
for(int i = 0; i <= n; i ++) p[i] = i;
int idx = 0;
for(int i = 0; i < n; i ++)
for(int j = i + 1; j < n; j ++)
{
int d = abs(points[i][0] - points[j][0]) + abs(points[i][1] - points[j][1]);
e[idx ++ ] = {
i, j, d};
}
return kruskal(idx);
}
};
Prim 更加适合稠密图
const int N = 1010, INF = 0x3f3f3f3f;
class Solution {
public:
int g[N][N];
int dist[N];
bool st[N] = {
false};
int prim(int n)
{
memset(dist, 0x3f, sizeof dist);
int res = 0;
for(int i = 0; i < n; i ++)
{
int t = -1;
for(int j = 0; j < n; j ++)
if(!st[j] && (t == -1 || dist[j] < dist[t]))
t = j;
if(i && dist[t] == INF) return INF;
if(i) res += dist[t];
st[t] = true;
for(int i = 0; i < n; i ++)
dist[i] = min(dist[i], g[t][i]);
}
return res;
}
int minCostConnectPoints(vector<vector<int>>& points) {
int n = points.size();
memset(g, 0x3f, sizeof g);
for(int i = 0; i < n; i ++)
for(int j = i + 1; j < n; j ++)
{
int d = abs(points[i][0] - points[j][0]) + abs(points[i][1] - points[j][1]);
g[i][j] = d;
g[j][i] = d;
}
return prim(n);
}
};
边栏推荐
- Do not refresh the page content, change the browser access address URL
- arduino上传程序出错不成功常见的问题解决
- HCIP第六天笔记
- 1. 在 SAP ABAP 事物码 SEGW 里创建 SAP OData 项目
- VC中获取窗体句柄的各种方法
- 【Day 2】机器阅读理解——常见机器阅读理解模型(上)
- Flutter RenderFlex overflowed by pixels on the bottom键盘弹出警告异常
- GPU资源池的虚拟化路径
- 一文带你读懂深度学习中的张量(tensor)是什么,它的运算是怎样的,如何理解张量,张量的维度,浅显易懂
- [untitled]
猜你喜欢
随机推荐
This article takes you to understand what tensors are in deep learning, how their operations are, how to understand tensors, and the dimensions of tensors, which are easy to understand
Huawei switch SEP double half ring design scheme and detailed configuration steps
Win11卸载程序在哪里?Win11卸载软件的两种方法
LeetCode(剑指 Offer)- 45. 把数组排成最小的数
Lesson 3: stock trading III
[server data recovery] a data recovery case in which a brand of MSA SAN storage RAID5 is paralyzed and the upper Lun cannot be used
Win11安全中心删除的文件如何恢复?
HCIP第五天笔记
EMQX Cloud 更新:新增 Redis 和 JWT 外部认证授权
What happens when you unplug the power? Gaussdb (for redis) dual life keeps you prepared
Record a detailed penetration test of a site
Hcip second experiment
HCIP第六天笔记
Don't underestimate websocket! Long connection, stateful, bidirectional and full duplex are all Wang's skills
密码密钥硬编码检查
Extract the language you need from the multilingual data set and save it as CSV
Séquence de traversée de l'ordre initial et de l'ordre moyen pour déterminer un arbre binaire (restauration de l'arbre binaire)
Hcip day 5 experiment
Linear table concept
Lesson 3: shortest distance









![[Go]二、RESTful API介紹和API流程和代碼結構](/img/fd/8ae3d6a4c0d0c973ce81672c1c529c.png)