Path of greater than equal to k length | Practice | GeeksforGeeks

Given a graph, a source vertex in the graph, and a number k, find if there is a simple path, of path length greater than or equal to k,(without any cycle) starting from a given source and ending at any other vertex.
Source vertex should always be  0.
Example 1:
Input:
V = 4 , E = 3 and K = 8
A[] = [0, 1, 5, 1, 2, 1, 2, 3, 1]
Output: 0
Explanation:
There exists no path which has a distance 
of 8. 

 
Example 2:
Input:
V = 9, E = 14 and K = 60
A[] = [0, 1, 4, 0, 7, 8, 1, 2, 8, 1, 7, 
11, 2, 3, 7, 2, 5, 4, 2, 8, 2, 3, 4, 9, 
3, 5, 14, 4, 5, 10, 5, 6, 2, 6, 7, 1, 6, 
8, 6, 7, 8, 7]
Output: 0
Explanation:

 



Your Task:  
You don't need to read input or print anything. Your task is to complete the function pathMoreThanK() which takes the integer V, Edges E, an integer K and Array A which is having (Source, Destination, Weight) as input parameters and returns 1 if the path of at least k distance exists, else returns 0.

Expected Time Complexity: O(N!)
Expected Auxiliary Space: O(N)
 
Constraints:
2 ≤ V ≤ 5
1 ≤ E ≤ 20
1 ≤ K ≤ 100
0
putyavka4 days ago
struct EE { int u, d; };
vector<int> F;
bool dfs(vector<vector<EE>>& G, int v, int d, int k) {
    if (F[v]) return false;
    if (d >= k) return true;
    F[v] = 1;
    for (auto &e: G[v])
        if (dfs(G, e.u, d+e.d, k)) return true;
    F[v] = 0;
    return false;
}
bool pathMoreThanK(int V, int E, int k, int *a) { 
    vector<vector<EE>> G(V); F.resize(V);
    for (int i = 0; i < E; i++)
        G[a[i*3]].push_back({a[i*3+1],a[i*3+2]}),
        G[a[i*3+1]].push_back({a[i*3],a[i*3+2]});
    return dfs(G, 0, 0, k);
} 
0
kvatsadeo6 days ago
public:
   bool helper(int i,int k,vector<bool> &vis,vector<pair<int,int>> adj[]){
        if(vis[i]==true) return false;
        if(k<=0) return true;
        vis[i]=1;
        for(auto it:adj[i]){
            if(!vis[it.first]){
                if(helper(it.first,k-it.second,vis,adj)) return true;
            }
        }
        vis[i]=0;
        return false;
    }
    bool pathMoreThanK(int V, int E, int k, int *a) 
    { 
       //  Code Here
       vector<pair<int,int>> adj[V];
       int n=3*E;
       for(int i=0;i<=n-3;i+=3){
           int u=a[i];
           int v=a[i+1];
           int wt=a[i+2];
           adj[u].push_back({v,wt});
           adj[v].push_back({u,wt});
       }
    //   for(int i=0;i<V;i++){
    //       for(auto it:adj[i]){
    //           cout<<i<<" "<<it.first<<"-->"<<it.second<<endl;
    //       }
    //   }
       vector<bool> vis(V,0);
       return helper(0,k,vis,adj);
    } 
};
0
vickyupadhyay361 week ago
Easy C++ Code using DFS 
int dfs(vector<pair<int,int>> adj[],vector<int> &vis,int &k,int &m,int dis,int node){
        if(m>=k) return 1;
        vis[node]=1;
        int i,wt;
        for(auto x:adj[node]){
            i=x.first;
            wt=x.second;
            if(!vis[i]){
                m=max(m,dis+wt);
                if(dfs(adj,vis,k,m,dis+wt,i)) return 1;
            }
        }
        vis[node]=0;
        return 0;
    }
    bool pathMoreThanK(int V, int E, int k, int *a) 
    { 
       //  Code Here
       vector<pair<int,int>> adj[V];
       int n=3*E;
       for(int i=0;i<n-2;i+=3){
           adj[a[i]].push_back({a[i+1],a[i+2]});
           adj[a[i+1]].push_back({a[i],a[i+2]});
       }
       
      vector<int> vis(V,0);
      int dis=0,m=0;
      return dfs(adj,vis,k,m,dis,0);
    } 
0
shubhamkulkarni16291 week ago
bool dfs(int node,vector<pair<int,int>>adj[],vector<int>&visited,int &ans,int k){          visited[node]=1;     if(ans>=k)         return true;              for(auto x:adj[node])     {         //base case         if(!visited[x.first])         {         ans=ans+x.second;         if(dfs(x.first,adj,visited,ans,k))         {             return true;         }           ans=ans-x.second;                    }           }         visited[node]=0;     return false;}
   bool pathMoreThanK(int V, int E, int k, int *a)    {       //  Code Here      vector<pair<int,int>>adj[V+1];           for(int i=0; i<3*E-2; i+=3)      {          adj[a[i]].push_back({a[i+1], a[i+2]});          adj[a[i+1]].push_back({a[i],a[i+2]});      }            vector<int>visited(V,0);      int ans=0;           if(dfs(0,adj,visited,ans,k))                {                  return true;              }                 return false;   } };
0
roboto7o32oo32 weeks ago
Clean C++ Solution ✨ ✨
 
class Solution {

public:

    bool solve(vector<vector<vector<int>>>& AList, unordered_set<int>& visited, int k, int u){
        if(visited.count(u)){
            return false;
        }
        if(k <= 0){
            return true;
        }
        
        visited.insert(u);
        
        for(vector<int> edge : AList[u]){
            if(solve(AList, visited, k-edge[1], edge[0])){
                return true;
            }
        }
        
        visited.erase(u);
        
        return false;
    }

    bool pathMoreThanK(int V, int E, int k, int *a) 
    { 
        vector<vector<vector<int>>> AList(V, vector<vector<int>>());
        
        for(int i=0; i<E; i++){
            int u = a[3*i];
            int v = a[3*i + 1];
            int w = a[3*i + 2];
            
            AList[u].push_back({v,w});
            AList[v].push_back({u,w});
        }
        
        unordered_set<int> visited;
        
        return solve(AList, visited, k, 0);
    } 
};
+1
ashishkyjp113 weeks ago
// Easy C++ code (really very easy)

void dfs(vector<pair<int, int>> g[], vector<int> &path, int key, int cost, int k, bool &ans)
    {
        if(ans)
            return;
        path[key]=1;
        
        if(cost>=k)
        {
            ans=true;
            return;
        }
        
        for(auto a:g[key])
        {
            if(!path[a.first])
            {
                // path[key]=0;
                // return;
                
                dfs(g, path, a.first, cost+a.second, k, ans);
            }
            
        }
        
        
        path[key]=0;
    }

    bool pathMoreThanK(int V, int E, int k, int *a) 
    { 
       //  Code Here
    //   dfgjhkfdgh
       vector<pair<int, int>> g[V+1];
       vector<int> path(V+1);
       bool ans=false;
       
       for(int i=0; i<3*E-2; i+=3)
       {
           g[a[i]].push_back({a[i+1], a[i+2]});
           g[a[i+1]].push_back({a[i],a[i+2]});
       }
       
       dfs(g, path, 0, 0, k, ans);
       
       return ans;
    } 
 
 
0
shivaraj54 weeks ago
//simple recursive solution
bool solve(int src,int k,vector<pair<int,int>>adj[],vector<bool>&vis){    vis[src]=true;    if(k<=0)     return true;    for(auto x:adj[src]){     int v=x.first;     int w=x.second;     if(vis[v])//it there is a cycle      continue;     if(w>=k)      return true;     if(solve(v,k-w,adj,vis))      return true;    }    vis[src]=false;    return false;   }   bool pathMoreThanK(int n, int m, int k, int *a) {       //  Code Here       vector<pair<int,int>>adj[n];       for(int i=0;i<m;i++){           adj[*a].push_back({*(a+1),*(a+2)});           adj[*(a+1)].push_back({*a,*(a+2)});           a=a+3;       }       vector<bool>vis(n,0);       return solve(0,k,adj,vis);   } 
0
bishtrockkak1 month ago
int ans;
    void dfs(vector<vector<pair<int,int>>>&g,int node,vector<int>&vis,int path){
        ans=max(ans,path);
        for(auto i : g[node]){
            if(vis[i.first]==1){
                continue;
            }
            else{
                vis[i.first]=1;
                dfs(g,i.first,vis,path+i.second);
                vis[i.first]=0;
            }
        }
    }
0
kartik_ey_11 month ago
  void dfs(vector<pair<int,int>>adj[] , vector<bool>&visited , int &count, int k , int h , int node){       if(h>=k)count++;    visited[node]=true;    for(auto m:adj[node]){        if(!visited[m.first]){            dfs(adj,visited,count , k , h+m.second , m.first);        }    }    visited[node]=false;   }   bool pathMoreThanK(int V, int E, int k, int *a)    {       //  Code Here             vector<pair<int,int>>adj[V];      for(int i=0;i<3*E-2;i+=3){          adj[a[i]].push_back({a[i+1],a[i+2]});          adj[a[i+1]].push_back({a[i],a[i+2]});      }      vector<bool>visited(V,0);      int count = 0;      int r=0;      dfs(adj,visited,count,k,r,0);      return count;   }  
+4
dev1713 months ago
SIMPLE C++ DFS + BACKTRACKING

    bool solve(int node,int path,int k,vector<pair<int,int>>adj[],vector<int>vis){
        if(path>=k)
        return true;
             vis[node]=1;
        for(auto it:adj[node]){
            if(!vis[it.first]){
                if(solve(it.first,path+it.second,k,adj,vis))
                return true;
            }
        }
             vis[node]=0;
        return false;
    }
    bool pathMoreThanK(int v, int e, int k, int *a) 
    { 
       //  Code Here
       vector<pair<int,int>>adj[v];
       for(int i=0;i<3*e-2;i+=3){
           adj[a[i]].push_back({a[i+1],a[i+2]});
           adj[a[i+1]].push_back({a[i],a[i+2]});
       }
       vector<int>vis(v,0);
       vis[0]=1;
      return solve(0,0,k,adj,vis);
    } 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.