当前位置:网站首页>leetcode 1584. 连接所有点的最小费用
leetcode 1584. 连接所有点的最小费用
2022-08-08 11:09:00 【来深圳】
Prim算法实现
class Solution {
public:
int minCostConnectPoints(vector<vector<int>> &points) {
int ans = 0;
int n = points.size();
// 定义并初始化邻接矩阵
vector<vector<int> > graph(n, vector<int>(n));
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
int manhattan = abs(points[i][0] - points[j][0]) + abs(points[i][1] - points[j][1]);
graph[i][j] = manhattan;
graph[j][i] = manhattan;
}
}
// isJoined 记录 各节点是否加入树
// distance 记录 各节点加入树的最低代价
vector<bool> isJoined(n);
vector<int> distance(n);
distance = vector<int>(graph[0]);
int p = n;
while (p > 0) {
p--;
// 选出未加入的 最小代价的 边
int minDistance = INT32_MAX;
int index = 0;
for (int i = 0; i < n; ++i) {
if (!isJoined[i]) {
if (distance[i] < minDistance) {
minDistance = distance[i];
index = i;
}
}
}
isJoined[index] = true;
ans += minDistance;
// 更新distance
for (int i = 0; i < n; ++i) {
if (distance[i] > graph[index][i]) {
distance[i] = graph[index][i];
}
}
}
return ans;
}
};
Prim算法时间复杂度O(|V|^2), 适用于边稠密图 (|E|大)
Kruskal算法
class Edge {
public:
Edge(int w, int s, int e) {
this->weight = w;
this->start = s;
this->end = e;
}
int weight;
int start;
int end;
};
bool compare(Edge e1, Edge e2) {
return e1.weight < e2.weight;
}
class UnionFind {
public:
vector<int> nums;
vector<int> rank;
explicit UnionFind(int n) {
this->nums = vector<int>(n);
this->rank = vector<int>(n);
for (int i = 0; i < nums.size(); ++i) {
this->nums[i] = i;
this->rank[i] = 1;
}
}
void merge(int i, int j) {
int p = find(j);
int q = find(i);
if (rank[p] > rank[q]) {
swap(p, q);
}
this->nums[p] = q;
this->rank[q] += this->rank[p];
}
int find(int x) {
if (x == nums[x]) {
return x;
}
return find(nums[x]);
}
bool check(int i, int j) {
return find(i) == find(j);
}
};
class Solution {
public:
int minCostConnectPoints(vector<vector<int>> &points) {
vector<Edge> edges;
int n = points.size();
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
int distance = abs(points[i][0] - points[j][0]) + abs(points[i][1] - points[j][1]);
edges.emplace_back(distance, i, j);
}
}
sort(edges.begin(), edges.end(), compare);
UnionFind uf(n);
for (auto & edge : edges) {
int w = edge.weight;
int start = edge.start;
int end = edge.end;
if (!uf.check(start, end)) {
uf.merge(start, end);
ans += w;
}
}
return ans;
}
};
Kruskal算法的时间复杂度为O(ElogE), Kruskal算法适用于边稀疏而顶点较多的图。
边栏推荐
- 五、树结构
- 我用开天平台做了一个城市防疫政策查询系统【开天aPaaS大作战】
- 消防安全知识培训讲座
- LeetCode 219. Repeating Elements II (2022.08.07)
- 图数据库是使用什么作为数据模型的呢?
- 5S软件就是将软件应用全维度简单化的软件系统
- (kali - elevated privileges 】 【 4.2.4) social engineering toolkit: remote control trojans use, set up and use
- Alibaba微服务组件Nacos注册中心
- 【Force】Add two numbers
- Flutter Game Tutorial Recreate the famous T-Rex game with Flutter and Flame
猜你喜欢
随机推荐
京东云无线宝产品部负责人张晓东 : 京东云无线宝与开源的亲密关系 | 《大神详解开源 BUFF 增益攻略》讲座回顾...
基于ftp协议的上传与下载
day02 -DOM—高级事件(注册事件、事件监听、删除事件、DOM事件流、事件对象、阻止默认行为、阻止事件冒泡、事件委托)—常用鼠标事件—常用的键盘事件
键值数据库中可以对值进行查询嘛?
学习笔记:CS520 Knowledge Graphs
oracle存储过程中表名变量的异常
ReentrantLock源码分析和使用案例
文档数据库中的文档可以有相同的数据结构嘛?
Mysql索引优化实战
力扣(LeetCode)219. 存在重复元素 II(2022.08.07)
【C语言】[编程题]倒置字符串
便利贴--48{再次,适配屏幕宽高class}
微服务分库分表
PG核心篇--物理存储结构
为什么说键值数据库有高可扩展性呢?
部署spark2.2集群(standalone模式)
持久化键值数据库的数据是保存在内存中吗?
关系数据库是怎么确定关系表中的数据的呢?
萤石、小米对垒智能摄像头
ReentrantLock原理,ReentrantLock和synchronized区别









