Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- /*
- given a connected undirectional unweighted graph
- you need to find the shortest distance between source node and destination node
- twist is you can only travel if they are having visitable as 1, we will be given at max 1 node with zero val
- another twist is the initial zero node is infected ,it can spread its virus to nodes near to k levels
- now find the shortest distance between source node and destination node
- */
- /**
- important point to understand here -
- you need to find the graph after the infection has been spread and then think about the shortest distance
- */
- /*
- //inputs for visitable and graph and N and infectedNode and k
- 1 1 1 1 1 1 1 1 1 1 1 1 1 1
- 14 17
- 0 1
- 0 5
- 1 2
- 5 6
- 2 3
- 6 7
- 6 12
- 3 7
- 3 4
- 7 8
- 12 13
- 8 4
- 13 9
- 9 10
- 10 8
- 10 11
- 11 4
- 11
- 3
- 1
- expected output -
- The result is 7
- */
- @SuppressWarnings({"unused","unchecked"})
- public class A20250208b_atlassianOA {
- static Scanner sc = new Scanner(System.in);
- private static int[] getArray() {
- String[] sArr = sc.nextLine().split(" ");
- int[] arr = Arrays.stream(sArr).mapToInt(Integer::parseInt).toArray();
- return arr;
- }
- private static char[] getCharArray() {
- String[] sArr = sc.nextLine().split(" ");
- char[] cArr = new char[sArr.length];
- for (int i = 0; i < sArr.length; i++) {
- cArr[i] = sArr[i].charAt(0); // Take the first character of each string
- }
- return cArr;
- }
- private static int getMax(int[] arr) {
- int currMax = Integer.MIN_VALUE;
- for (int curr : arr) {
- currMax = Math.max(currMax, curr);
- }
- return currMax;
- }
- public static void main(String args[]) {
- // prepare the inputs
- int[] visitable = getArray();
- int[] tempArr = getArray();
- int nodes = tempArr[0];
- int edges = tempArr[1];
- ArrayList<Integer>[] adjList =(ArrayList<Integer>[]) new ArrayList[nodes];//explicit type conversion
- for(int i =0;i<nodes;i+=1){
- adjList[i] = new ArrayList<>();
- }
- int temp = edges;
- //fill up the graph
- while(temp!=0){
- temp-=1;
- tempArr = getArray();
- int n1 = tempArr[0];
- int n2 = tempArr[1];
- adjList[n1].add(n2);
- adjList[n2].add(n1);
- }
- //lets spoil the visitability by spreading infection
- int n = getArray()[0];
- int infectedNode = getArray()[0];
- int k = getArray()[0];
- int[] visited;
- int[] level;
- Queue<Integer> q;
- //assign new values for v l q
- visited = new int[nodes];
- level = new int[nodes];
- Arrays.fill(level,-2);
- q = new LinkedList<>();
- visited[infectedNode] = 1;
- visitable[infectedNode] = 0;
- level[infectedNode] = 0;
- q.offer(infectedNode);
- while(!q.isEmpty()){
- int parent = q.poll();
- if(level[parent]==k){
- continue;//you dont need further infection spread
- }
- for(int child:adjList[parent]){
- if(visitable[child]==1 && visited[child] == 0){
- visitable[child] = 0;
- visited[child] = 1;
- level[child] = level[parent]+1;
- q.offer(child);
- }
- }
- }
- //assign new values for v l q after infection
- visited = new int[nodes];
- level = new int[nodes];
- Arrays.fill(level,-2);
- q = new LinkedList<>();
- if(visitable[0] == 1){
- visited[0] = 1;
- level[0] = 0;
- q.offer(0);
- }
- while(!q.isEmpty()){
- int parent = q.poll();
- for(int child:adjList[parent]){
- if(visitable[child]==1 && visited[child] == 0){
- visited[child] = 1;
- level[child] = level[parent]+1;
- q.offer(child);
- }
- }
- }
- int res = level[n];
- System.out.println("The result is " + res);
- }
- }
- class Pair{
- int row;
- int col;
- public Pair(int i,int j){
- this.row = i;
- this.col = j;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement