Advertisement
Kali_prasad

Untitled

Feb 9th, 2025
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.01 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. /*
  4.  
  5. given a connected undirectional unweighted graph
  6. you need to find the shortest distance between source node and destination node
  7. twist is you can only travel if they are having visitable as 1
  8.  
  9.  */
  10. /**
  11.  important point to understand here -
  12.  
  13. you can do a simple bfs , but you will visit the nodes only if they are visitable
  14. otherwise no
  15.  
  16.  */
  17.  /*
  18.  
  19. //inputs for visitable and graph
  20. 1 1 0 1 1
  21. 5 5
  22. 0 1
  23. 0 2
  24. 1 3
  25. 2 4
  26. 3 4
  27. 4
  28.  
  29. expected output -
  30. The result is 3
  31.  
  32.  
  33.  */
  34.  
  35.  
  36. @SuppressWarnings({"unused","unchecked"})
  37. public class A20250208a_atlassianOA  {
  38.  
  39.     static Scanner sc = new Scanner(System.in);
  40.  
  41.     private static int[] getArray() {
  42.         String[] sArr = sc.nextLine().split(" ");
  43.         int[] arr = Arrays.stream(sArr).mapToInt(Integer::parseInt).toArray();
  44.         return arr;
  45.     }
  46.  
  47.     private static char[] getCharArray() {
  48.         String[] sArr = sc.nextLine().split(" ");
  49.         char[] cArr = new char[sArr.length];
  50.         for (int i = 0; i < sArr.length; i++) {
  51.             cArr[i] = sArr[i].charAt(0); // Take the first character of each string
  52.         }
  53.         return cArr;
  54.     }
  55.  
  56.     private static int getMax(int[] arr) {
  57.         int currMax = Integer.MIN_VALUE;
  58.         for (int curr : arr) {
  59.             currMax = Math.max(currMax, curr);
  60.         }
  61.         return currMax;
  62.     }
  63.  
  64.     public static void main(String args[]) {
  65.         // prepare the inputs
  66.  
  67.  
  68.         int[] visitable = getArray();
  69.         int[] tempArr = getArray();
  70.  
  71.         int nodes = tempArr[0];
  72.         int edges = tempArr[1];
  73.         ArrayList<Integer>[] adjList =(ArrayList<Integer>[]) new ArrayList[nodes];//explicit type conversion
  74.         for(int i =0;i<nodes;i+=1){
  75.             adjList[i] = new ArrayList<>();
  76.         }
  77.         int temp = edges;
  78.         //fill up the graph
  79.         while(temp!=0){
  80.             temp-=1;
  81.             tempArr = getArray();
  82.             int n1 = tempArr[0];
  83.             int n2 = tempArr[1];
  84.             adjList[n1].add(n2);
  85.             adjList[n2].add(n1);
  86.         }
  87.  
  88.         Queue<Integer> q = new LinkedList<>();
  89.         int[] level = new int[nodes];
  90.         Arrays.fill(level,(int)1e8);//filling level with some big number
  91.         int[] visited = new int[nodes];
  92.  
  93.         if(visitable[0]==1){
  94.             visited[0] = 1;
  95.             level[0] = 0;
  96.             q.offer(0);//offer is safe than add
  97.         }
  98.  
  99.         while(!q.isEmpty()){
  100.             int parent = q.poll();
  101.             for(int child:adjList[parent]){
  102.                 if(visitable[child]==1 && visited[child]==0){
  103.                     visited[child]=1;
  104.                     level[child] = level[parent]+1;
  105.                     q.offer(child);
  106.                 }
  107.             }
  108.         }
  109.         tempArr = getArray();
  110.         int res = level[tempArr[0]];
  111.        
  112.         System.out.println("The result is " + res);
  113.  
  114.     }
  115. }
  116.  
  117. class Pair{
  118.     int row;
  119.     int col;
  120.     public Pair(int i,int j){
  121.         this.row = i;
  122.         this.col = j;
  123.        
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement