当前位置: 首页 > 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/98256.html

相关文章:

  • 网站建设与推广的销售属于b2b的网站有哪些
  • 网站开发费税率是多少乐云seo官网
  • php网站建设网站线上销售怎么做推广
  • 天琥网页设计培训宁波seo网络推广咨询热线
  • vue单页面做网站加载慢搜索引擎收录查询
  • 网站制作程序网络推广运营外包公司
  • led网站制作品牌推广包括哪些内容
  • 网站建设审批程序seo草根博客
  • 网站 开发网站项目开发流程
  • 优质做网站市场推广
  • 一个好的网站建设如何做好网络营销管理
  • wordpress产品插件百度网盘优化
  • 打字赚钱平台 学生一单一结铁岭网站seo
  • 网站开发需要哪些资料长沙做网站推广
  • 家具网站建设方案百度快速收录3元一条
  • 重庆联通的网站建设成都专业的整站优化
  • html购物网站设计论文seo怎么优化效果更好
  • 汉中微信网站建设公司沈阳疫情最新消息
  • 可以接单做3d网站环球网疫情最新动态
  • 做菠菜网站判多久百度站长工具
  • 上海专业网站设计sem专业培训公司
  • 校园图书回收网站建设软考十大最靠谱it培训机构
  • 成都市青羊区城乡建设局网站推广公司哪家好
  • 开发动态网站价格网络营销的特点举例说明
  • 做系统后之前网站怎么找回打开百度网页版
  • 做网站公司哪家比较好哪里有竞价推广托管
  • dw中怎样做网站二级页面潍坊网站定制模板建站
  • 上海哪家公司做网站比较好网络广告推广方法
  • 品牌建设公司网站专门制作小程序的公司
  • 手机p2p网站开发营业推广促销方式有哪些