📜  连接城市中所有房屋所需的最低成本

📅  最后修改于: 2021-05-17 17:10:04             🧑  作者: Mango

给定一个由N个2D坐标{x,y}组成的2D数组house [] [] ,其中每个坐标代表每个房屋的位置,任务是找到连接城市所有房屋的最低成本。

例子:

方法:想法是根据给定的信息创建一个加权图,其中任意一对边之间的权重等于连接它们的成本,即C i,即两个坐标之间的曼哈顿距离。生成图形后,应用Kruskal算法使用不相交集找到图形的最小生成树。最后,打印最低成本。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
vector parent, size;
 
// Utility function to find set of an
// element v using path compression
// technique
int find_set(int v)
{
    // If v is the parent
    if (v == parent[v])
        return v;
 
    // Otherwsie, recursively
    // find its parent
    return parent[v]
           = find_set(parent[v]);
}
 
// Function to perform union
// of the sets a and b
int union_sets(int a, int b)
{
    // Find parent of a and b
    a = find_set(a);
    b = find_set(b);
 
    // If parent are different
    if (a != b) {
 
        // Swap Operation
        if (size[a] < size[b])
            swap(a, b);
 
        // Update parent of b as a
        parent[b] = a;
        size[a] += size[b];
        return 1;
    }
 
    // Otherwise, return 0
    return 0;
}
 
// Function to create a Minimum Cost
// Spanning tree for given houses
void MST(int houses[][2], int n)
{
    // Stores adjacency list of graph
    vector > > v;
 
    // Traverse each coordinate
    for (int i = 0; i < n; i++) {
 
        for (int j = i + 1; j < n; j++) {
 
            // Find the Manhattan distance
            int p = abs(houses[i][0]
                        - houses[j][0]);
 
            p += abs(houses[i][1]
                     - houses[j][1]);
 
            // Add the edges
            v.push_back({ p, { i, j } });
        }
    }
 
    parent.resize(n);
    size.resize(n);
 
    // Sort all the edges
    sort(v.begin(), v.end());
 
    // Initialize parent[] and size[]
    for (int i = 0; i < n; i++) {
        parent[i] = i, size[i] = 1;
    }
 
    /// Stores the minimum cost
    int ans = 0;
 
    // Finding the minimum cost
    for (auto x : v) {
 
        // Perform the unioun operation
        if (union_sets(x.second.first,
                       x.second.second)) {
            ans += x.first;
        }
    }
 
    // Print the minimum cost
    cout << ans;
}
 
// Driver Code
int main()
{
    // Given houses
    int houses[][2] = { { 0, 0 }, { 2, 2 },
                        { 3, 10 }, { 5, 2 },
                        { 7, 0 }};
 
    int N = sizeof(houses)
            / sizeof(houses[0]);
 
    // Function Call
    MST(houses, N);
 
    return 0;
}


Python3
# Python3 program for the above approach
parent = [0] * 100
size = [0] * 100
 
# Utility function to find set of an
# element v using path compression
# technique
def find_set(v):
     
    # If v is the parent
    if (v == parent[v]):
        return v
 
    # Otherwsie, recursively
    # find its parent
    parent[v] = find_set(parent[v])
 
    return parent[v]
 
# Function to perform union
# of the sets a and b
def union_sets(a, b):
     
    # Find parent of a and b
    a = find_set(a)
    b = find_set(b)
 
    # If parent are different
    if (a != b):
         
        # Swap Operation
        if (size[a] < size[b]):
            a, b = b, a
 
        # Update parent of b as a
        parent[b] = a
        size[a] += size[b]
        return 1
 
    # Otherwise, return 0
    return 0
 
# Function to create a Minimum Cost
# Spanning tree for given houses
def MST(houses, n):
     
    # Stores adjacency list of graph
    v = []
 
    # Traverse each coordinate
    for i in range(n):
        for j in range(i + 1, n):
 
            # Find the Manhattan distance
            p = abs(houses[i][0] -
                    houses[j][0])
 
            p += abs(houses[i][1] -
                     houses[j][1])
 
            # Add the edges
            v.append([p, i, j])
 
    # Sort all the edges
    v = sorted(v)
 
    # Initialize parent[] and size[]
    for i in range(n):
        parent[i] = i
        size[i] = 1
 
    # Stores the minimum cost
    ans = 0
 
    # Finding the minimum cost
    for x in v:
 
        # Perform the unioun operation
        if (union_sets(x[1], x[2])):
            ans += x[0]
             
    # Print the minimum cost
    print(ans)
 
# Driver Code
if __name__ == '__main__':
     
    # Given houses
    houses = [ [ 0, 0 ], [ 2, 2 ],
               [ 3, 10 ], [ 5, 2 ],
               [ 7, 0 ] ]
 
    N = len(houses)
 
    # Function Call
    MST(houses, N)
 
# This code is contributed by mohit kumar 29


输出:
20


时间复杂度: O(N 2 )
辅助空间: O(N 2 )