给你一个points 数组,表示 2D 平面上的一些点,其中 points[i] = [xi, yi] 。
连接点 [xi, yi] 和点 [xj, yj] 的费用为它们之间的 曼哈顿距离 :|xi - xj| + |yi - yj| ,其中 |val| 表示 val 的绝对值。
请你返回将所有点连接的最小总费用。只有任意两点之间 有且仅有 一条简单路径时,才认为所有点都已连接。
示例 1:
输入:points = [[0,0],[2,2],[3,10],[5,2],[7,0]]
输出:20
解释:
我们可以按照上图所示连接所有点得到最小总费用,总费用为 20 。
注意到任意两个点之间只有唯一一条路径互相到达。
示例 2:
输入:points = [[3,12],[-2,5],[-4,1]]
输出:18
示例 3:
输入:points = [[0,0],[1,1],[1,0],[-1,1]]
输出:4
示例 4:
输入:points = [[-1000000,-1000000],[1000000,1000000]]
输出:4000000
示例 5:
输入:points = [[0,0]]
输出:0
提示:
1 <= points.length <= 1000
-106 <= xi, yi <= 106
所有点 (xi, yi) 两两不同。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/min-cost-to-connect-all-points
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
这是一道标准的最小生成树的题目,方法自然有克鲁苏卡尔算法(Kruskal),以及prim算法,这里我用的是Kruskal算法,基本思路就是将所有的边放入数组中,每次都找到最短的边,并利用并查集把连接在一起的点逻辑上合并,最后生成n-1条边结束,代码如下:
class Solution {
private:
vector<int> parent;
int part = 0;
public:
struct Edge{
int a, b, w;
bool operator<(const Edge& e) const {
return w < e.w;
}
};
int minCostConnectPoints(vector<vector<int>>& points) {
int n = points.size();
if(n <= 1) return 0;
//connections每一行是两个顶点和权重
vector<Edge> connections;
for(int i = 0; i < n; i ++){
for(int j = i + 1; j < n; j ++){
connections.push_back({i, j,
abs(points[i][0] - points[j][0]) + abs(points[i][1] - points[j][1])});
}
}
return minimumCost(n, connections);
}
int minimumCost(int n, vector<Edge>& connections) {
part = n;
parent = vector<int>(n, 0);
for (int i = 0; i < n; ++i)
parent[i] = i;
//把所有边按权值进行排序
sort(connections.begin(), connections.end());
int res = 0;
for (Edge& conn : connections) {
int pa = find(conn.a);
int pb = find(conn.b);
if (pa != pb) {
uni(conn.a, conn.b);
res += conn.w;
}
if (part == 1)
return res;
}
return -1;
}
int find(int a) {
if (parent[a] == a)
return a;
parent[a] = find(parent[a]);
return parent[a];
}
void uni(int a, int b) {
int pa = find(a);
int pb = find(b);
if (pa == pb)
return;
parent[pa] = pb;
part --;
}
};
下面是超时的算法,但是非常全面,代码如下:
// 构建边的结构体
struct edge{
int u, v; // 边的两个端点编号
int cost; // 边的权值
};
// 边的比较函数
bool cmp(edge a, edge b){
return a.cost < b.cost;
}
class Solution {
private:
vector<int> parent;
int part = 0;
public:
// 并查集查询函数,返回x所在集合的根结点
int findFather(vector<int> father, int x) {
int a = x;
while (x != father[x]){//找到头的位置,返回头的位置(认爹,直到自己是自己的爹)
x = father[x];
}
//更新father参数
int z;
while (a != father[a]){
z = a;
a = father[a];
father[z] = x;
}
//x = father[x];
return x;
}
// Kruskal算法求无向图的最小生成树
void Kruskal(int n, int m, vector<edge> &E, vector<edge> &res, int &totalCost){
// n:顶点个数
// m:边的个数
// m:边的个数
// E:边的合集
vector<int> father(n);
int faU, faV;
for (int i = 0; i < n; ++i){
father[i] = i;
}
sort(E.begin(), E.end(), cmp);
for (int i = 0; i < m; ++i){
faU = findFather(father, E[i].u);
faV = findFather(father, E[i].v);
if (faU != faV){
res.push_back(E[i]);
father[faU] = faV;
totalCost += E[i].cost;
// if (NumEdge == n-1) break; // 如果原图是由多个区域组成,则注释这一句
}
}
}
int minCostConnectPoints(vector<vector<int>>& points) {
int n = points.size();
if(n <= 1) return 0;
//connections每一行是两个顶点和权重
vector<edge> connections;
for(int i = 0; i < n; i ++){
for(int j = i + 1; j < n; j ++){
connections.push_back({i, j,
abs(points[i][0] - points[j][0]) + abs(points[i][1] - points[j][1])});
}
}
int totalCost = 0;
vector<edge> res;
Kruskal(points.size(), connections.size(), connections, res, totalCost);
return totalCost;
}
};