当前位置:网站首页>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算法适用于边稀疏而顶点较多的图。
边栏推荐
猜你喜欢
leetcode:761. 特殊的二进制序列【递归 + 转换有效括号】
Combining "xPlus" to discuss the innovation and change of software architecture
带你深入理解3.4.2的版本更新,对用户带来了什么?
.NET Community Toolkit 8.0.0 版本发布
卫星互联网真能替代 5G?
office安装出现了“office对安装源的访问被拒绝30068-4(5)”错误
300万招标!青岛市医疗保障局主机数据库中间件运行维护服务项目
上海控安SmartRocket系列产品推介(二):SmartRocket Modeler可视化建模开发工具
shell 创建LVM逻辑据卷
在SAP分析云里根据业务数据绘制词云(Word Cloud)
随机推荐
软件测试之测试代表用户
微服务负载均衡器LoadBalancer实战
office安装出现了“office对安装源的访问被拒绝30068-4(5)”错误
微服务负载均衡器Ribbon实战
有哪些典型的列存储数据库呢?
[Horizon Rising Sun X3 Trial Experience] WIFI connection, SSH login, TogetherROS installation (section 2)
shell之常用小工具
刷题《剑指Offer》day12
一文读懂配置管理(CM)
GC explanation and tuning of JVM
Leetcode 105. 从前序与中序遍历序列构造二叉树
About the Celery service report under win Process 'Worker' exited with 'exitcode 1' [duplicate]
搞清楚系统到底怎样支撑高并发以及架构图的绘制(面试向)
ets declarative ui development, how to get the current system time
PG核心篇--物理存储结构
Some optional strategies and usage scenarios for PWA application Service Worker caching
LeetCode_487_最大连续1的个数Ⅱ
部署spark2.2集群(standalone模式)
2G 3G 4G 5G 基站覆盖范围
ReentrantReadWriteLock读写锁和票据锁StempedLock