Advertisement
Kali_prasad

Untitled

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