📜  参观位于环形路周线所有节点的最低成本路径

📅  最后修改于: 2021-04-24 20:40:29             🧑  作者: Mango

给定圆的圆周数组pos [] ,该数组标记圆上N个点相对于固定点沿顺时针方向的距离。我们必须找到可以访问所有点的最小距离。我们可以从任何一点开始。

例子:

方法 :

为了解决上述问题,我们必须遵循以下步骤:

  • 对标记圆上N个点的距离的数组进行排序。
  • 通过将N个元素的值加上arr [i + n] =周长+ arr [i]来使数组大小增加两次。
  • 求出值i的所有有效迭代的最小值(arr [i +(n-1)] – arr [i])

下面是上述方法的实现:

C++
// C++ implementation to find the 
// Minimum Cost Path to visit all nodes
// situated at the Circumference of 
// Circular Road
  
#include 
using namespace std;
  
// Function to find the minimum cost
int minCost(int arr[], int n, int circumference)
{
    // Sort the given array
    sort(arr, arr + n);
  
    // Initialise a new array of double size
    int arr2[2 * n];
  
    // Fill the array elements
    for (int i = 0; i < n; i++) {
        arr2[i] = arr[i];
        arr2[i + n] = arr[i] + circumference;
    }
  
    // Find the minimum path cost
    int res = INT_MAX;
  
    for (int i = 0; i < n; i++)
        res = min(res, arr2[i + (n - 1)] - arr2[i]);
  
    // Return the final result
    return res;
}
  
// Driver code
int main()
{
    int arr[] = { 19, 3, 6 };
  
    int n = sizeof(arr) / sizeof(arr[0]);
  
    int circumference = 20;
  
    cout << minCost(arr, n, circumference);
  
    return 0;
}


Java
// Java implementation to find the 
// Minimum Cost Path to visit all nodes
// situated at the Circumference of 
// Circular Road
import java.util.*; 
import java. util. Arrays;
  
class GFG { 
  
// Function to find the minimum cost
static int minCost(int arr[], int n,
                   int circumference)
{
    // Sort the given array
    Arrays.sort(arr);
  
    // Initialise a new array of double size
    int[] arr2 = new int[2 * n];
  
    // Fill the array elements
    for(int i = 0; i < n; i++)
    {
       arr2[i] = arr[i];
       arr2[i + n] = arr[i] + circumference;
    }
  
    // Find the minimum path cost
    int res = Integer.MAX_VALUE;
  
    for(int i = 0; i < n; i++)
       res = Math.min(res, 
                      arr2[i + (n - 1)] -
                      arr2[i]);
  
    // Return the final result
    return res;
}
  
// Driver code
public static void main(String args[])
{
    int arr[] = { 19, 3, 6 };
    int n = arr.length;
    int circumference = 20;
  
    System.out.println(minCost(arr, n, 
                               circumference));
}
  
}
  
// This code is contributed by ANKITKUMAR34


Python3
# Python3 implementation to find the 
# minimum cost path to visit all nodes
# situated at the circumference of 
# circular Road
  
# Function to find the minimum cost
def minCost(arr, n, circumference):
  
    # Sort the given array
    arr.sort()
  
    #Initialise a new array of double size
    arr2 = [0] * (2 * n)
  
    # Fill the array elements
    for i in range(n):
        arr2[i] = arr[i]
        arr2[i + n] = arr[i] + circumference
  
    # Find the minimum path cost
    res = 9999999999999999999;
  
    for i in range(n):
        res = min(res, 
                  arr2[i + (n - 1)] -
                  arr2[i]);
  
    # Return the final result
    return res;
  
# Driver code
arr = [ 19, 3, 6 ];
n = len(arr)
circumference = 20;
  
print(minCost(arr, n, circumference))
  
# This code is contributed by ANKITKUMAR34


C#
// C# implementation to find the 
// Minimum Cost Path to visit all nodes
// situated at the Circumference of 
// Circular Road
using System;
  
class GFG{ 
  
// Function to find the minimum cost
static int minCost(int []arr, int n,
                   int circumference)
{
    // Sort the given array
    Array.Sort(arr);
  
    // Initialise a new array of double size
    int[] arr2 = new int[2 * n];
  
    // Fill the array elements
    for(int i = 0; i < n; i++)
    {
       arr2[i] = arr[i];
       arr2[i + n] = arr[i] + circumference;
    }
  
    // Find the minimum path cost
    int res = int.MaxValue;
  
    for(int i = 0; i < n; i++)
       res = Math.Min(res, arr2[i + (n - 1)] -
                           arr2[i]);
  
    // Return the readonly result
    return res;
}
  
// Driver code
public static void Main(String []args)
{
    int []arr = { 19, 3, 6 };
    int n = arr.Length;
    int circumference = 20;
  
    Console.WriteLine(minCost(arr, n, circumference));
}
}
  
// This code is contributed by Princi Singh


输出:
7