当前位置: 首页 > wzjs >正文

对搜索引擎优化的认识优化落实疫情防控新十条

对搜索引擎优化的认识,优化落实疫情防控新十条,黄石本土做网站的公司,武汉做网站文章目录 问题描述解题思路方法1:Kruskal算法(推荐)代码实现(Kruskal Union-Find)复杂度分析 方法2:Prim算法代码实现(Prim算法)复杂度分析 1584. Min Cost to Connect All Points …

文章目录

  • 问题描述
  • 解题思路
    • 方法1:Kruskal算法(推荐)
      • 代码实现(Kruskal + Union-Find)
      • 复杂度分析
    • 方法2:Prim算法
      • 代码实现(Prim算法)
      • 复杂度分析

1584. Min Cost to Connect All Points
You are given an array points representing integer coordinates of some points on a 2D-plane, where points[i] = [xi, yi].

The cost of connecting two points [xi, yi] and [xj, yj] is the manhattan distance between them: |xi - xj| + |yi - yj|, where |val| denotes the absolute value of val.

Return the minimum cost to make all points connected. All points are connected if there is exactly one simple path between any two points.

问题描述

给定一组二维平面上的点,其中 points[i] = [xi, yi]。连接两点 [xi, yi][xj, yj] 的费用是它们之间的曼哈顿距离:|xi - xj| + |yi - yj|。返回连接所有点所需的最小总费用。所有点都应该被连接,并且任意两点之间有且只有一条路径。

Example 1:
在这里插入图片描述

Input: points = [[0,0],[2,2],[3,10],[5,2],[7,0]]
Output: 20

Explanation:

We can connect the points as shown above to get the minimum cost of 20.
Notice that there is a unique path between every pair of points.
在这里插入图片描述

Example 2:

Input: points = [[3,12],[-2,5],[-4,1]]
Output: 18

解题思路

这是一个典型的**最小生成树(MST)**问题,可以使用 Kruskal算法 或 Prim算法 解决。

方法1:Kruskal算法(推荐)

  • 计算所有边:生成所有可能的点对,并计算它们的曼哈顿距离。
  • 按权重排序边:从小到大排序所有边。
  • 使用并查集(Union-Find):
    • 初始化每个点为一个独立的集合。
    • 遍历排序后的边,如果两个点不在同一集合,就合并它们,并累加费用。
    • 直到所有点连通(即边的数量 = n-1,其中 n 是点的数量)。

代码实现(Kruskal + Union-Find)

#include <vector>
#include <algorithm>
using namespace std;class UnionFind {
private:vector<int> parent;vector<int> rank;
public:UnionFind(int n) {parent.resize(n);rank.resize(n, 0);for (int i = 0; i < n; i++) {parent[i] = i;}}int find(int u) {if (parent[u] != u) {parent[u] = find(parent[u]); // Path compression}return parent[u];}bool unionSets(int u, int v) {int rootU = find(u);int rootV = find(v);if (rootU == rootV) return false; // Already connectedif (rank[rootU] > rank[rootV]) {parent[rootV] = rootU;} else if (rank[rootU] < rank[rootV]) {parent[rootU] = rootV;} else {parent[rootV] = rootU;rank[rootU]++;}return true;}
};class Solution {
public:int minCostConnectPoints(vector<vector<int>>& points) {int n = points.size();vector<vector<int>> edges; // {cost, u, v}// Generate all possible edgesfor (int i = 0; i < n; i++) {for (int j = i + 1; j < n; j++) {int cost = abs(points[i][0] - points[j][0]) + abs(points[i][1] - points[j][1]);edges.push_back({cost, i, j});}}// Sort edges by costsort(edges.begin(), edges.end());UnionFind uf(n);int res = 0;int edgesUsed = 0;for (auto& edge : edges) {int cost = edge[0], u = edge[1], v = edge[2];if (uf.unionSets(u, v)) {res += cost;edgesUsed++;if (edgesUsed == n - 1) break;}}return res;}
};

复杂度分析

  • 时间复杂度: O ( n ² l o g n ) O(n^² log n) O(n²logn)(排序边)。
  • 空间复杂度: O ( n ² ) O(n^²) O(n²)(存储所有边)。

方法2:Prim算法

  • 初始化优先队列(最小堆):从任意一个点开始,将其所有邻边加入堆。
  • 逐步扩展MST:
    • 每次取出权重最小的边,如果该边连接的点未被访问,则加入MST,并累加费用。
    • 将该点的所有邻边加入堆。
  • 直到所有点都被访问。

代码实现(Prim算法)

#include <vector>
#include <queue>
#include <climits>
using namespace std;class Solution {
public:int minCostConnectPoints(vector<vector<int>>& points) {int n = points.size();vector<bool> visited(n, false);priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> minHeap;// Start with node 0minHeap.push({0, 0});int res = 0;int edgesUsed = 0;while (edgesUsed < n) {auto [cost, u] = minHeap.top();minHeap.pop();if (visited[u]) continue;visited[u] = true;res += cost;edgesUsed++;// Add all edges from u to unvisited nodesfor (int v = 0; v < n; v++) {if (!visited[v]) {int newCost = abs(points[u][0] - points[v][0]) + abs(points[u][1] - points[v][1]);minHeap.push({newCost, v});}}}return res;}
};

复杂度分析

  • 时间复杂度: O ( n ² l o g n ) O(n^² log n) O(n²logn)(优先队列操作)。
  • 空间复杂度: O ( n ) O(n) O(n)(存储 visited 和优先队列)。
http://www.dtcms.com/wzjs/239415.html

相关文章:

  • 网站更改备案信息网络运营培训哪里有学校
  • 汽车网站策划百度广告联盟一个月能赚多少
  • 做网站建设分哪些类型自助建站系统源码
  • 做saas网站可行吗北京网络营销推广培训哪家好
  • 官方网站重要性百度识别图片找图
  • 闸北建设机械网站网站建设推广公司
  • 做一个软件需要哪些技术win优化大师
  • 企业宣传类网站建设搜狗搜索引擎优化
  • tomcat加jsp做网站最有效的app推广方式有哪些
  • 济南做网站哪里便宜苏州seo排名优化课程
  • 工业网站建设微信营销推广软件
  • 网站开发项目小组成员职责推广app平台有哪些
  • 新闻网站怎么做缓存网站优化排名软件
  • 怎么在网上找接单做网站的公司百度搜索使用方法
  • h5网站系统刚开的店铺怎么做推广
  • 怎么做全网小说网站成人电脑基础培训班
  • 我想自己做网站吗网站的seo是什么意思
  • 热点新闻事件及评论2023网站优化基本技巧
  • wordpress完全静态化插件网站优化的意义
  • 去哪里学做网站app百度推广账号
  • wap源码之家郑州网站seo外包公司
  • 网站开发一个月求几个微信推广平台
  • 高端品牌网站建设公司搜索引擎营销与seo优化
  • 旅游网站建设流程广告公司起名大全最新
  • 建设网站成都成都做整站优化
  • 医院网站建设运营方案大连中小企业网络营销
  • 佛山外贸网站个人网页制作成品
  • 香港的网站不需要备案吗百度竞价查询
  • 设计接单兼职网站抖音广告推广
  • 杨凌做网站网址谷歌浏览器官网下载安装