📌  相关文章
📜  连接以数组表示的加权节点的最小成本

📅  最后修改于: 2022-05-13 01:57:52.871000             🧑  作者: Mango

连接以数组表示的加权节点的最小成本

给定一个包含 N 个元素(节点)的数组,其中每个元素都是该节点的权重。连接两个节点将采用它们权重的乘积成本。您必须将每个节点与每个其他节点(直接或间接)连接起来。输出所需的最低成本。
例子:

Input : a[] = {6, 2, 1, 5}
Output :  13
Explanation : 
Here, we connect the nodes as follows:
connect a[0] and a[2], cost = 6*1 = 6,
connect a[2] and a[1], cost = 1*2 = 2,
connect a[2] and a[3], cost = 1*5 = 5.
every node is reachable from every other node:
Total cost = 6+2+5 = 13.

Input  : a[] = {5, 10}
Output : 50
Explanation : connections:
connect a[0] and a[1], cost = 5*10 = 50,
Minimum cost = 50. 

我们需要做一些观察,我们必须制作一个具有 N-1 条边的连通图。由于输出将是两个数字的乘积之和,因此我们必须最小化该求和方程中每一项的乘积。我们该怎么做?显然,选择数组中的最小元素并将其相互连接。通过这种方式,我们可以从特定节点到达所有其他节点。
设,最小元素 = a[i],(设它的索引为 0)
最低成本
所以,答案是最小元素的乘积和除最小元素之外的所有元素的总和。

C++
// cpp code for Minimum Cost Required to connect weighted nodes
#include 
using namespace std;
int minimum_cost(int a[], int n)
{
    int mn = INT_MAX;
    int sum = 0;
    for (int i = 0; i < n; i++) {
 
        // To find the minimum element
        mn = min(a[i], mn);
 
        // sum of all the elements
        sum += a[i];
    }
 
    return mn * (sum - mn);
}
 
// Driver code
int main()
{
    int a[] = { 4, 3, 2, 5 };
    int n = sizeof(a) / sizeof(a[0]);
    cout << minimum_cost(a, n) << endl;
    return 0;
}


Java
// Java code for Minimum Cost Required to
// connect weighted nodes
 
import java.io.*;
 
class GFG {
     
    static int minimum_cost(int a[], int n)
    {
         
        int mn = Integer.MAX_VALUE;
        int sum = 0;
         
        for (int i = 0; i < n; i++) {
 
            // To find the minimum element
            mn = Math.min(a[i], mn);
 
            // sum of all the elements
            sum += a[i];
        }
 
        return mn * (sum - mn);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int a[] = { 4, 3, 2, 5 };
        int n = a.length;
         
        System.out.println(minimum_cost(a, n));
    }
}
 
// This code is contributed by vt_m.


Python 3
# Python 3 code for Minimum Cost
# Required to connect weighted nodes
import sys
 
def minimum_cost(a, n):
 
    mn = sys.maxsize
    sum = 0
    for i in range(n):
 
        # To find the minimum element
        mn = min(a[i], mn)
 
        # sum of all the elements
        sum += a[i]
 
    return mn * (sum - mn)
 
# Driver code
if __name__ == "__main__":
     
    a = [ 4, 3, 2, 5 ]
    n = len(a)
    print(minimum_cost(a, n))
 
# This code is contributed
# by ChitraNayal


C#
// C# code for Minimum Cost Required
// to connect weighted nodes
using System;
 
class GFG {
     
    // Function to calculate minimum cost
    static int minimum_cost(int []a, int n)
    {
         
        int mn = int.MaxValue;
        int sum = 0;
         
        for (int i = 0; i < n; i++)
        {
 
            // To find the minimum element
            mn = Math.Min(a[i], mn);
 
            // sum of all the elements
            sum += a[i];
        }
 
        return mn * (sum - mn);
    }
 
    // Driver code
    public static void Main()
    {
        int []a = {4, 3, 2, 5};
        int n = a.Length;
         
        Console.WriteLine(minimum_cost(a, n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:

24