📜  最低成本图

📅  最后修改于: 2021-05-04 11:39:42             🧑  作者: Mango

给定的二维平面上的N个节点表示为(x i ,y i ) 。如果节点之间的曼哈顿距离为1,则称这些节点已连接。您可以连接两个未连接的节点,但要以它们之间的欧式距离为代价。任务是连接图,以使每个节点都有一条从任何节点到其路径的路径,且成本最低。

例子:

方法:蛮力方法是将每个节点与每个其他节点连接,并且对于其他N个节点也是如此,但是在最坏的情况下,时间复杂度将为N N。
另一种方法是找到具有欧几里得距离的每对顶点的成本,并且那些相连的对的成本为0
在知道每对的代价之后,我们将对最小生成树应用Kruskal算法,它将产生连接图的最小代价。请注意,对于Kruskal算法,您必须具有不交集集合并集(DSU)的知识。

下面是上述方法的实现:

C++
// C++ implentation of the approach
#include 
using namespace std;
 
// Max number of nodes given
const int N = 500 + 10;
 
// arr is the parent array
// sz is the size of the
// subtree in DSU
int arr[N], sz[N];
 
// Function to initilize the parent
// and size array for DSU
void initialize()
{
    for (int i = 1; i < N; ++i) {
        arr[i] = i;
        sz[i] = 1;
    }
}
 
// Function to return the
// parent of the node
int root(int i)
{
    while (arr[i] != i)
        i = arr[i];
    return i;
}
 
// Function to perform the
// merge operation
void unin(int a, int b)
{
    a = root(a);
    b = root(b);
 
    if (a != b) {
        if (sz[a] < sz[b])
            swap(a, b);
 
        sz[a] += sz[b];
        arr[b] = a;
    }
}
 
// Function to return the minimum cost required
double minCost(vector >& p)
{
 
    // Number of points
    int n = (int)p.size();
 
    // To store the cost of every possible pair
    // as { cost, {to, from}}.
    vector > > cost;
 
    // Calculating the cost of every possible pair
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if (i != j) {
 
                // Getting Manhattan distance
                int x = abs(p[i].first - p[j].first)
                        + abs(p[i].second - p[j].second);
 
                // If the distance is 1 the cost is 0
                // or already connected
                if (x == 1) {
                    cost.push_back({ 0, { i + 1, j + 1 } });
                    cost.push_back({ 0, { j + 1, i + 1 } });
                }
                else {
 
                    // Calculating the euclidean distance
                    int a = p[i].first - p[j].first;
                    int b = p[i].second - p[j].second;
                    a *= a;
                    b *= b;
                    double d = sqrt(a + b);
                    cost.push_back({ d, { i + 1, j + 1 } });
                    cost.push_back({ d, { j + 1, i + 1 } });
                }
            }
        }
    }
 
    // Krushkal Algorithm for Minimum
    // spanning tree
    sort(cost.begin(), cost.end());
 
    // To initialize the size and
    // parent array
    initialize();
 
    double ans = 0.00;
    for (auto i : cost) {
        double c = i.first;
        int a = i.second.first;
        int b = i.second.second;
 
        // If the parents are different
        if (root(a) != root(b)) {
            unin(a, b);
            ans += c;
        }
    }
 
    return ans;
}
 
// Driver code
int main()
{
 
    // Vector pairs of points
    vector > points = {
        { 1, 1 },
        { 2, 2 },
        { 2, 3 }
    };
 
    // Function calling and printing
    // the answer
    cout << minCost(points);
 
    return 0;
}


Java
// Java implentation of the approach
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG{
     
// Max number of nodes given
static int N = 500 + 10;
 
static class pair
{
    double c;
    int first, second;
     
    pair(double c, int first, int second)
    {
        this.c = c;
        this.first = first;
        this.second = second;
    }
}
 
// arr is the parent array
// sz is the size of the
// subtree in DSU
static int[] arr = new int[N],
              sz = new int[N];
  
// Function to initilize the parent
// and size array for DSU
static void initialize()
{
    for(int i = 1; i < N; ++i)
    {
        arr[i] = i;
        sz[i] = 1;
    }
}
  
// Function to return the
// parent of the node
static int root(int i)
{
    while (arr[i] != i)
        i = arr[i];
         
    return i;
}
  
// Function to perform the
// merge operation
static void unin(int a, int b)
{
    a = root(a);
    b = root(b);
  
    if (a != b)
    {
        if (sz[a] < sz[b])
        {
            int tmp = a;
            a = b;
            b = tmp;
        }
         
        sz[a] += sz[b];
        arr[b] = a;
    }
}
  
// Function to return the minimum
// cost required
static double minCost(int[][] p)
{
     
    // Number of points
    int n = (int)p.length;
  
    // To store the cost of every possible pair
    // as { cost, {to, from}}.
    ArrayList cost = new ArrayList<>();
  
    // Calculating the cost of every possible pair
    for(int i = 0; i < n; ++i)
    {
        for(int j = 0; j < n; ++j)
        {
            if (i != j)
            {
                 
                // Getting Manhattan distance
                int x = Math.abs(p[i][0] - p[j][0]) +
                        Math.abs(p[i][1] - p[j][1]);
  
                // If the distance is 1 the cost is 0
                // or already connected
                if (x == 1)
                {
                    cost.add(new pair( 0, i + 1,
                                          j + 1 ));
                    cost.add(new pair( 0, j + 1,
                                          i + 1 ));
                }
                else
                {
                     
                    // Calculating the euclidean
                    // distance
                    int a = p[i][0] - p[j][0];
                    int b = p[i][1] - p[j][1];
                    a *= a;
                    b *= b;
                     
                    double d = Math.sqrt(a + b);
                    cost.add(new pair(d, i + 1,
                                         j + 1 ));
                    cost.add(new pair(d, j + 1,
                                         i + 1));
                }
            }
        }
    }
  
    // Krushkal Algorithm for Minimum
    // spanning tree
    Collections.sort(cost, new Comparator<>()
    {
        public int compare(pair a, pair b)
        {
            if(a.c <= b.c)
                return -1;
            else
                return 1;
        }
    });
  
    // To initialize the size and
    // parent array
    initialize();
  
    double ans = 0.00;
    for(pair i : cost)
    {
        double c = i.c;
        int a = i.first;
        int b = i.second;
  
        // If the parents are different
        if (root(a) != root(b))
        {
            unin(a, b);
            ans += c;
        }
    }
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
     
    // Vector pairs of points
    int[][] points = { { 1, 1 },
                       { 2, 2 },
                       { 2, 3 }};
     
    // Function calling and printing
    // the answer
    System.out.format("%.5f", minCost(points));
}
}
 
// This code is contributed by offbeat


输出:
1.41421