当前位置:网站首页>LeetCode 1584. Minimum cost of connecting all points
LeetCode 1584. Minimum cost of connecting all points
2022-07-16 08:57:00 【HumbleFool】
LeetCode 1584. The minimum cost of connecting all points 
Kruskal More suitable for sparse graph
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 More suitable for dense graphs
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);
}
};
边栏推荐
- Hongliulin, data gold mine has been dug up in the coal mine!
- What if the win11 touchpad doesn't work? The solution of win11 touch panel not working
- VRRP basic configuration
- The computer regularly clears wechat data
- 红柳林,煤矿里挖出了数据金矿!
- 【历史上的今天】7 月 13 日:数据库之父逝世;苹果公司购买 CUPS 代码;IBM 芯片联盟
- 快讯:每日优鲜回应3天关闭9城业务;名创优品将在港交所上市
- How to apply for PMP project management certification examination?
- PG operation and maintenance -- service start and stop
- Either retire, change careers, or change management. PS hasn't blogged for two months
猜你喜欢

1. Create SAP OData project in SAP ABAP transaction code segw

Judge whether two binary trees are isomorphic, and three implementation methods (recursion, queue, stack)

如何报考PMP项目管理认证考试?

Running process of program

面试诈骗:竟然还有靠面试挣钱的公司

【服务器数据恢复】某品牌MSA SAN存储RAID5瘫痪,上层LUN无法使用的数据恢复案例

VRRP basic configuration

Preorder and inorder traversal sequences determine a binary tree (restore binary tree)

不会真有人觉得聊天机器人难吧——使用BERT加载预训练模型得到中文句子向量

不会真有人觉得聊天机器人难吧——微调BERT模型得到句子间的相似度
随机推荐
电脑共享文件打不开要如何解决
EMQX Cloud 更新:新增 Redis 和 JWT 外部认证授权
LeetCode(剑指 Offer)- 45. 把数组排成最小的数
【Day 2】机器阅读理解——常见机器阅读理解模型(上)
What if the win11 touchpad doesn't work? The solution of win11 touch panel not working
内存映射原理及详解(非常实用)
Preorder and inorder traversal sequences determine a binary tree (restore binary tree)
[untitled]
先序和中序遍曆序列確定一顆二叉樹(還原二叉樹)
HCIP第二天笔记
【历史上的今天】7 月 13 日:数据库之父逝世;苹果公司购买 CUPS 代码;IBM 芯片联盟
I used Kaitian platform to build an urban epidemic prevention policy inquiry system. Don't you try it?
JMeter 21天打卡 day01
程序员转型项目管理?考个PMP?
HCIP第五天实验
HCIP第五天笔记
Inclined stack - principle and Implementation
程序的运行过程
Running process of program
【无标题】