diff --git a/cpp/2204/220426.cpp b/cpp/2204/220426.cpp new file mode 100644 index 0000000..f20f9dc --- /dev/null +++ b/cpp/2204/220426.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include + +class DisjointSet { +private: + std::vector parent; + +public: + explicit DisjointSet(int n) { + parent.reserve(n); + for (int i = 0; i < n; ++i) + parent.push_back(i); + } + + int Find(int x) { + return (parent[x] == x) ? x : (parent[x] = Find(parent[x])); + } + + void Union(int x, int y) { + auto xR = Find(x), yR = Find(y); + if (xR != yR) + parent[xR] = yR; + } + +}; + +struct TupleCmp { + bool operator()(const std::tuple& x, const std::tuple& y) const { + return std::get<2>(x) > std::get<2>(y); + } +}; + +/** + * 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. + */ + +class Solution { +public: + static int minCostConnectPoints(std::vector>& points) { + int n = points.size(); + if (n <= 1) + return 0; + + std::vector> edgePairs; + for (int i = 0; i < n; ++i) + for (int j = i + 1; j < n; ++j) + edgePairs.emplace_back(i, j, std::abs(points[i][0] - points[j][0]) + std::abs(points[i][1] - points[j][1])); + + std::priority_queue, std::vector>, TupleCmp> q(edgePairs.begin(), edgePairs.end()); + DisjointSet s(n); + + int ans = 0, cnt = 0; + while (cnt < n - 1) { + auto cur = q.top(); + q.pop(); + auto i = std::get<0>(cur); + auto j = std::get<1>(cur); + if (s.Find(i) != s.Find(j)) { + s.Union(i, j); + ans += std::get<2>(cur); + ++cnt; + } + } + + return ans; + } +}; diff --git a/cpp/2204/CMakeLists.txt b/cpp/2204/CMakeLists.txt index 813ae3c..102bf18 100644 --- a/cpp/2204/CMakeLists.txt +++ b/cpp/2204/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2204) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2204 220425-CN.cpp) +ADD_EXECUTABLE(2204 220426.cpp)