Advertisement
Kali_prasad

Untitled

Feb 9th, 2025
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.35 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, we will be given at max 1 node with zero val
  8. another twist is the initial zero node is infected ,it can spread its virus to nodes near to k levels
  9. now find the shortest distance between source node and destination node
  10.  */
  11. /**
  12.  important point to understand here -
  13.  
  14. you need to find the graph after the infection has been spread and then think about the shortest distance
  15.  
  16.  
  17.  */
  18.  /*
  19.  
  20. //inputs for visitable and graph and N and infectedNode and k
  21. 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  22. 14 17
  23. 0 1
  24. 0 5
  25. 1 2
  26. 5 6
  27. 2 3
  28. 6 7
  29. 6 12
  30. 3 7
  31. 3 4
  32. 7 8
  33. 12 13
  34. 8 4
  35. 13 9
  36. 9 10
  37. 10 8
  38. 10 11
  39. 11 4
  40. 11
  41. 3
  42. 1
  43.  
  44. expected output -
  45. The result is 7
  46.  
  47.  
  48.  */
  49.  
  50.  
  51. @SuppressWarnings({"unused","unchecked"})
  52. public class A20250208b_atlassianOA  {
  53.  
  54.     static Scanner sc = new Scanner(System.in);
  55.  
  56.     private static int[] getArray() {
  57.         String[] sArr = sc.nextLine().split(" ");
  58.         int[] arr = Arrays.stream(sArr).mapToInt(Integer::parseInt).toArray();
  59.         return arr;
  60.     }
  61.  
  62.     private static char[] getCharArray() {
  63.         String[] sArr = sc.nextLine().split(" ");
  64.         char[] cArr = new char[sArr.length];
  65.         for (int i = 0; i < sArr.length; i++) {
  66.             cArr[i] = sArr[i].charAt(0); // Take the first character of each string
  67.         }
  68.         return cArr;
  69.     }
  70.  
  71.     private static int getMax(int[] arr) {
  72.         int currMax = Integer.MIN_VALUE;
  73.         for (int curr : arr) {
  74.             currMax = Math.max(currMax, curr);
  75.         }
  76.         return currMax;
  77.     }
  78.  
  79.     public static void main(String args[]) {
  80.         // prepare the inputs
  81.  
  82.  
  83.         int[] visitable = getArray();
  84.         int[] tempArr = getArray();
  85.  
  86.         int nodes = tempArr[0];
  87.         int edges = tempArr[1];
  88.         ArrayList<Integer>[] adjList =(ArrayList<Integer>[]) new ArrayList[nodes];//explicit type conversion
  89.         for(int i =0;i<nodes;i+=1){
  90.             adjList[i] = new ArrayList<>();
  91.         }
  92.         int temp = edges;
  93.         //fill up the graph
  94.         while(temp!=0){
  95.             temp-=1;
  96.             tempArr = getArray();
  97.             int n1 = tempArr[0];
  98.             int n2 = tempArr[1];
  99.             adjList[n1].add(n2);
  100.             adjList[n2].add(n1);
  101.         }
  102.         //lets spoil the visitability by spreading infection
  103.         int n = getArray()[0];
  104.         int infectedNode = getArray()[0];
  105.         int k = getArray()[0];
  106.  
  107.         int[] visited;
  108.         int[] level;
  109.         Queue<Integer> q;
  110.         //assign new values for v l q
  111.         visited = new int[nodes];
  112.         level = new int[nodes];
  113.         Arrays.fill(level,-2);
  114.         q = new LinkedList<>();
  115.  
  116.  
  117.         visited[infectedNode] = 1;
  118.         visitable[infectedNode] = 0;
  119.         level[infectedNode] = 0;
  120.         q.offer(infectedNode);
  121.  
  122.         while(!q.isEmpty()){
  123.             int parent = q.poll();
  124.             if(level[parent]==k){
  125.                 continue;//you dont need further infection spread
  126.             }
  127.             for(int child:adjList[parent]){
  128.                 if(visitable[child]==1 && visited[child] == 0){
  129.                     visitable[child] = 0;
  130.                     visited[child] = 1;
  131.                     level[child] = level[parent]+1;
  132.                     q.offer(child);
  133.                 }
  134.             }
  135.         }
  136.  
  137.  
  138.         //assign new values for v l q after infection
  139.         visited = new int[nodes];
  140.         level = new int[nodes];
  141.         Arrays.fill(level,-2);
  142.         q = new LinkedList<>();
  143.  
  144.         if(visitable[0] == 1){
  145.             visited[0] = 1;
  146.             level[0] = 0;
  147.             q.offer(0);
  148.         }
  149.  
  150.         while(!q.isEmpty()){
  151.             int parent = q.poll();
  152.            
  153.             for(int child:adjList[parent]){
  154.                 if(visitable[child]==1 && visited[child] == 0){
  155.                     visited[child] = 1;
  156.                     level[child] = level[parent]+1;
  157.                     q.offer(child);
  158.                 }
  159.             }
  160.         }
  161.  
  162.         int res = level[n];
  163.  
  164.  
  165.         System.out.println("The result is " + res);
  166.  
  167.     }
  168. }
  169.  
  170. class Pair{
  171.     int row;
  172.     int col;
  173.     public Pair(int i,int j){
  174.         this.row = i;
  175.         this.col = j;
  176.        
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement