JAVA Simple solution *************


#1

Compublic class Solution {

ArrayList<ArrayList<CellBody>>G;
int ans;
boolean isTargetFound;

public class CellBody{
    public int v;//value
    public int w;//weight
    public char t;// Edge type
    
    CellBody(int v, int w, char t){
        this.v=v;
        this.w=w;
        this.t=t;
    }
}

public void dfs(int s, int target, boolean[] visited, int tillCost, int noOfEEdges){
    //base case 
    if(noOfEEdges==2){
        return;
    }else if(target==s){
        isTargetFound=true;
        ans = Math.min(ans, tillCost);
        return;
    }
    
    //process the curr node
    visited[s]=true;
    
    //go for childer
    for(CellBody ch : G.get(s)){
        if(!visited[ch.v]){
            if(ch.t=='E'){
                dfs(ch.v,target, visited, tillCost+ch.w, noOfEEdges+1);
            }else{
                dfs(ch.v,target, visited, tillCost+ch.w, noOfEEdges);
            }
        }
    }
    //post process node
    visited[s]=false;
}

public int solve(int A, ArrayList<ArrayList<Integer>> B, int C, int D, ArrayList<ArrayList<Integer>> E) {
    
    ans = Integer.MAX_VALUE;
    isTargetFound = false;
    G = new ArrayList<ArrayList<CellBody>>();
    boolean[] visited = new boolean[A+1];
    
    for(int i=0;i<=A;i++){
        G.add(new ArrayList<CellBody>());
    }
    
    //B[i][0] ---> B[i][1] digraph
    for(int i=0; i<B.size(); i++){
        int a = B.get(i).get(0);
        int b = B.get(i).get(1);
        int w = B.get(i).get(2);
        G.get(a).add(new CellBody(b,w,'B'));
    }
    
    //E[i][0] ---- E[i][1], no of node can greather than A
    for(int i=0; i<E.size(); i++){
        int a = E.get(i).get(0);
        int b = E.get(i).get(1);
        int w = E.get(i).get(2);
        if(1<=a && 1<=b && a<=A && b<=A){
            G.get(a).add(new CellBody(b,w,'E'));
            G.get(b).add(new CellBody(a,w,'E'));
        }

    }
    
    //dfs travesal 
    dfs(C, D, visited, 0, 0);
    return isTargetFound ? ans : -1;
}

}
ment body goes here.