📜  权重> 0的边的乘积最小的路径

📅  最后修改于: 2021-04-22 04:14:21             🧑  作者: Mango

给定一个有N个节点和E个边的有向图,其中每个边的权重> 0 ,还给出了源S和目标D。任务是找到从SD的边的乘积最小的路径。如果没有从SD的路径,则打印-1

例子:

方法:想法是使用Bellman ford算法。这是因为Dijkstra的算法在这里不能使用,因为它仅适用于非负边缘。这是因为在[0-1]之间乘以值时,乘积会无限期地减小,最后返回0。
此外,需要检测循环,因为如果存在一个循环,则该循环的乘积将无限期地将乘积减小为0,并且乘积将趋于0。为简单起见,我们将报告此类循环。
可以按照以下步骤计算结果:

  1. 初始化一个数组, dis []的初始值为’inf’,但dis [S]的值为1。
  2. 从1 – N-1循环运行。对于图中的每个边:
    • dis [edge.second] = min(dis [edge.second],dis [edge.first] * weight(edge))
  3. 对图形中的每个边缘运行另一个循环,如果任何边缘以(dis [edge.second]> dis [edge.first] * weight(edge))退出,则将检测到循环。
  4. 如果dist [d]为无穷大,则返回-1 ,否则返回dist [d]

下面是上述方法的实现:

C++
// C++ implementation of the approach.
#include 
using namespace std;
 
double inf = std::
    numeric_limits::infinity();
 
// Function to return the smallest
// product of edges
double bellman(int s, int d,
               vector,
                           double> >
                   ed,
               int n)
{
    // If the source is equal
    // to the destination
    if (s == d)
        return 0;
 
    // Array to store distances
    double dis[n + 1];
 
    // Initialising the array
    for (int i = 1; i <= n; i++)
        dis[i] = inf;
    dis[s] = 1;
 
    // Bellman ford algorithm
    for (int i = 0; i < n - 1; i++)
        for (auto it : ed)
            dis[it.first.second] = min(dis[it.first.second],
                                       dis[it.first.first]
                                           * it.second);
 
    // Loop to detect cycle
    for (auto it : ed) {
        if (dis[it.first.second]
            > dis[it.first.first] * it.second)
            return -2;
    }
 
    // Returning final answer
    if (dis[d] == inf)
        return -1;
    else
        return dis[d];
}
 
// Driver code
int main()
{
 
    int n = 3;
    vector, double> > ed;
 
    // Input edges
    ed = { { { 1, 2 }, 0.5 },
           { { 1, 3 }, 1.9 },
           { { 2, 3 }, 3 } };
 
    // Source and Destination
    int s = 1, d = 3;
 
    // Bellman ford
    double get = bellman(s, d, ed, n);
 
    if (get == -2)
        cout << "Cycle Detected";
    else
        cout << get;
}


Java
// Java implementation of the approach
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
 
class Pair
{
    K first;
    V second;
 
    public Pair(K first, V second)
    {
        this.first = first;
        this.second = second;
    }
}
 
class GFG{
 
static final float inf = Float.POSITIVE_INFINITY;
 
// Function to return the smallest
// product of edges
static float bellman(int s, int d,
                     ArrayList, Float>> ed,
                     int n)
{
     
    // If the source is equal
    // to the destination
    if (s == d)
        return 0;
 
    // Array to store distances
    float[] dis = new float[n + 1];
 
    // Initialising the array
    Arrays.fill(dis, inf);
    dis[s] = 1;
 
    // Bellman ford algorithm
    for(int i = 0; i < n - 1; i++)
        for(Pair, Float> it : ed)
            dis[it.first.second] = Math.min(dis[it.first.second],
                                            dis[it.first.first] *
                                                it.second);
 
    // Loop to detect cycle
    for(Pair, Float> it : ed)
    {
        if (dis[it.first.second] >
            dis[it.first.first] *
                it.second)
            return -2;
    }
 
    // Returning final answer
    if (dis[d] == inf)
        return -1;
    else
        return dis[d];
}
 
// Driver code
public static void main(String[] args)
{
    int n = 3;
     
    // Input edges
    ArrayList, Float>> ed = new ArrayList<>(
            Arrays.asList(
                      new Pair, Float>(
                           new Pair(1, 2), 0.5f),
                    new Pair, Float>(
                         new Pair(1, 3), 1.9f),
                    new Pair, Float>(
                         new Pair(2, 3), 3f)));
 
    // Source and Destination
    int s = 1, d = 3;
 
    // Bellman ford
    float get = bellman(s, d, ed, n);
 
    if (get == -2)
        System.out.println("Cycle Detected");
    else
        System.out.println(get);
}
}
 
// This code is contributed by sanjeev2552


Python3
# Python3 implementation of the approach.
import sys
 
inf = sys.maxsize;
 
# Function to return the smallest
# product of edges
def bellman(s, d, ed, n) :
 
    # If the source is equal
    # to the destination
    if (s == d) :
        return 0;
 
    # Array to store distances
    dis = [0]*(n + 1);
 
    # Initialising the array
    for i in range(1, n + 1) :
        dis[i] = inf;
         
    dis[s] = 1;
 
    # Bellman ford algorithm
    for i in range(n - 1) :
        for it in ed :
            dis[it[1]] = min(dis[it[1]], dis[it[0]] * ed[it]);
 
    # Loop to detect cycle
    for it in ed :
        if (dis[it[1]] > dis[it[0]] * ed[it]) :
            return -2;
 
    # Returning final answer
    if (dis[d] == inf) :
        return -1;
    else :
        return dis[d];
 
# Driver code
if __name__ == "__main__" :
 
    n = 3;
     
    # Input edges
    ed = { ( 1, 2 ) : 0.5 ,
        ( 1, 3 ) : 1.9 ,
        ( 2, 3 ) : 3 };
 
    # Source and Destination
    s = 1; d = 3;
 
    # Bellman ford
    get = bellman(s, d, ed, n);
 
    if (get == -2) :
        print("Cycle Detected");
    else :
        print(get);
 
# This code is contributed by AnkitRai01


输出:
1.5

时间复杂度: O(E * V)