📌  相关文章
📜  计算长度为 N 的步行次数,其中每次步行的成本等于给定数量

📅  最后修改于: 2021-10-25 05:16:53             🧑  作者: Mango

给定一个加权无向图,长度 N 和成本 X。任务是计算长度为 N 的不同步行 W 的数量,使得Cost(W) = X。
我们将步行 W 的成本定义为沿步行的边权重的最大值。
节点从 1 到 n 编号。该图不包含任何多边或自环。

例子:

  • 这个想法是预先计算no。对于所有可能成本的每个顶点的长度为 N 的步行,并将它们存储在一个二维矩阵中。让我们称这个矩阵为 B。这些值可以通过在给定的无向图上运行 DFS 来计算。
    例如,

矩阵 B 的给定快照显示了存储在其中的值。这里 B(i, j) 表示没有。从顶点 i 开始的长度为 N 的步行,其成本为步行 j。

  • 我们维护一个一维数组Maxedge ,其中保留长度为 N 的游走成本。当游走长度小于 N 并且有一些与边(u,v)相关的成本 X 时,我们调用相同的函数。
    我们为length == N设置一个基本条件,我们更新数组B并返回调用。
  • 计算矩阵 B 后,我们只需将所有具有cost = x的顶点的步行次数相加,即可计算总步行次数。

下面是上述方法的实现

C++
// C++ program to count the number of walks
// of length N where cost of each walk is
// equal to k
#include 
using namespace std;
int G[250][250] = {0};
int Maxedge[250] = {0};
int B[250][250] = {0};
int l = 0, n, m;
 
// Function return total
// walk of length N
int TotalWalks(int cost)
{
     int ans=0;
     
    // Add values of all
    // node with cost X
     for(int i=1;i<=n;i++)
     {
        ans+=B[i][cost];
     }
     
     return ans;
}
 
 
// Function to precompute array B
// meantioned above
void DFS(int u, int v,int len)
{
    // Base condition
    if (l == len)             
    {
        // Updating the matrix B when
        // we get a walk of length N.
        B[u][ Maxedge[len]]++;
        return ;
    }
    for (int i = 1; i <= n; i++)
    {
        if (G[v][i] !=0)
        {
            // Incrementing the length
            // of the walk
            l++;
             
            // Updating the cost of the walk
            Maxedge[l] = max(Maxedge[l - 1],
                             G[v][i]);
            DFS(u, i, len);
            l--;
        }
    }
}
 
// Function to calculate total
// number of walks of length N
void NumberOfWalks(int cost,int len)
{
    for (int i = 1; i <= n; i++)
    {
        // Calling the function DFS
        DFS(i, i, len); 
    }
     
    int ans = TotalWalks(cost);
     
    // Print the answer
    cout<< ans << endl;
}
 
// Driver code
int main()
{
    int Cost = 2;
    n = 3, m = 3;
    int length = 4;
     
    // Create a graph given in
    // the above diagram
    G[1][2] = 1;
    G[2][1] = 1;
    G[2][3] = 2;
    G[3][2] = 2;
    G[1][3] = 3;
    G[3][1] = 3;
     
    NumberOfWalks(Cost, length) ;
}


Java
// Java program to count the number of walks
// of length N where cost of each walk is
// equal to k
import java.util.*;
 
class GFG{
     
static int [][]G = new int[250][250];
static int []Maxedge = new int[250];
static int [][]B = new int[250][250];
static int l = 0, n, m;
 
// Function return total
// walk of length N
static int TotalWalks(int cost)
{
    int ans = 0;
     
    // Add values of all
    // node with cost X
    for(int i = 1; i <= n; i++)
    {
        ans += B[i][cost];
    }
     
    return ans;
}
 
// Function to precompute array B
// meantioned above
static void DFS(int u, int v, int len)
{
    // Base condition
    if (l == len)            
    {
         
        // Updating the matrix B when
        // we get a walk of length N.
        B[u][ Maxedge[len]]++;
        return;
    }
     
    for (int i = 1; i <= n; i++)
    {
        if (G[v][i] !=0)
        {
             
            // Incrementing the length
            // of the walk
            l++;
             
            // Updating the cost of the walk
            Maxedge[l] = Math.max(Maxedge[l - 1],
                                        G[v][i]);
            DFS(u, i, len);
            l--;
        }
    }
}
 
// Function to calculate total
// number of walks of length N
static void NumberOfWalks(int cost,int len)
{
    for(int i = 1; i <= n; i++)
    {
 
       // Calling the function DFS
       DFS(i, i, len);
    }
     
    int ans = TotalWalks(cost);
     
    // Print the answer
    System.out.print(ans + "\n");
}
 
// Driver code
public static void main(String[] args)
{
    int Cost = 2;
    n = 3; m = 3;
    int length = 4;
     
    // Create a graph given in
    // the above diagram
    G[1][2] = 1;
    G[2][1] = 1;
    G[2][3] = 2;
    G[3][2] = 2;
    G[1][3] = 3;
    G[3][1] = 3;
     
    NumberOfWalks(Cost, length);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to count the number of walks
# of length N where cost of each walk is
# equal to k
G = [[0 for i in range(250)]
        for j in range(250)]
Maxedge = [0 for i in range(250)]
B = [[0 for i in range(250)]
        for j in range(250)]
l = 0
n = 0
m = 0
  
# Function return total
# walk of length N
def TotalWalks(cost):
 
    ans = 0
      
    # Add values of all
    # node with cost X
    for i in range(1, n + 1):
        ans += B[i][cost]
      
    return ans
 
# Function to precompute array B
# meantioned above
def DFS(u, v, len):
     
    global l
     
    # Base condition
    if (l == len):
     
        # Updating the matrix B when
        # we get a walk of length N.
        B[u][ Maxedge[len]] += 1
        return
     
    for i in range(1, n + 1):
        if (G[v][i] != 0):
         
            # Incrementing the length
            # of the walk
            l += 1
              
            # Updating the cost of the walk
            Maxedge[l] = max(Maxedge[l - 1], G[v][i])
            DFS(u, i, len)
            l -= 1
         
# Function to calculate total
# number of walks of length N
def NumberOfWalks(cost, len):
     
    for i in range(1, n + 1):
     
        # Calling the function DFS
        DFS(i, i, len) 
     
    ans = TotalWalks(cost)
      
    # Print the answer
    print(ans)
     
# Driver code
if __name__=='__main__':
 
    Cost = 2
    n = 3
    m = 3
    length = 4
      
    # Create a graph given in
    # the above diagram
    G[1][2] = 1
    G[2][1] = 1
    G[2][3] = 2
    G[3][2] = 2
    G[1][3] = 3
    G[3][1] = 3
      
    NumberOfWalks(Cost, length)
 
# This code is contributed by rutvik_56


C#
// C# program to count the number of walks
// of length N where cost of each walk is
// equal to k
using System;
 
class GFG{
     
static int [,]G = new int[250, 250];
static int []Maxedge = new int[250];
static int [,]B = new int[250, 250];
static int l = 0, n;
 
// Function return total
// walk of length N
static int TotalWalks(int cost)
{
    int ans = 0;
     
    // Add values of all
    // node with cost X
    for(int i = 1; i <= n; i++)
    {
       ans += B[i, cost];
    }
    return ans;
}
 
// Function to precompute array B
// meantioned above
static void DFS(int u, int v, int len)
{
 
    // Base condition
    if (l == len)            
    {
         
        // Updating the matrix B when
        // we get a walk of length N.
        B[u, Maxedge[len]]++;
        return;
    }
     
    for(int i = 1; i <= n; i++)
    {
       if (G[v, i] != 0)
       {
            
           // Incrementing the length
           // of the walk
           l++;
            
           // Updating the cost of the walk
           Maxedge[l] = Math.Max(Maxedge[l - 1],
                                       G[v, i]);
           DFS(u, i, len);
           l--;
       }
    }
}
 
// Function to calculate total
// number of walks of length N
static void NumberOfWalks(int cost, int len)
{
    for(int i = 1; i <= n; i++)
    {
        
       // Calling the function DFS
       DFS(i, i, len);
    }
     
    int ans = TotalWalks(cost);
     
    // Print the answer
    Console.Write(ans + "\n");
}
 
// Driver code
public static void Main(String[] args)
{
    int Cost = 2;
    n = 3;
    int length = 4;
     
    // Create a graph given in
    // the above diagram
    G[1, 2] = 1;
    G[2, 1] = 1;
    G[2, 3] = 2;
    G[3, 2] = 2;
    G[1, 3] = 3;
    G[3, 1] = 3;
     
    NumberOfWalks(Cost, length);
}
}
 
// This code is contributed by gauravrajput1


Javascript


输出:
10

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程