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,
- another twist there are multiple nodes say g, are infected ,they can spread their 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 ,you cant spread infection
- by doing g bfs , rather than connect all of them to a universal node which is at level -1
- now you can easily spread the infection and then find the shortest distance between source node and destination node
- */
- /*
- //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 1 1
- 16 20
- 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
- 5 14
- 14 15
- 15 12
- 11
- 3 7
- 1
- expected output -
- The result is 8
- */
- @SuppressWarnings({"unused","unchecked"})
- public class A20250208c_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[] infectedNodes = getArray();
- int k = getArray()[0];
- int[] visited;
- int[] level;
- Queue<Integer> q;
- //assign new values for v l q and also consider the universal Node
- visited = new int[nodes+1];
- level = new int[nodes+1];
- Arrays.fill(level,-2);
- q = new LinkedList<>();
- int universalNode = nodes;
- ArrayList<Integer>[] adjList2 =(ArrayList<Integer>[]) new ArrayList[nodes+1];//explicit type conversion
- for(int i =0;i<nodes+1;i+=1){
- System.out.println("i is " + i);
- if(i==nodes){
- adjList2[i] = new ArrayList<>();
- continue;
- }
- System.out.println("i is after " + i);
- adjList2[i] = new ArrayList(adjList[i]);
- }
- System.out.println("after adjList2 filling " );
- for(int infectedNode : infectedNodes){
- adjList2[universalNode].add(infectedNode);
- adjList2[infectedNode].add(universalNode);
- }
- visited[universalNode] = 1;
- level[universalNode] = -1;
- q.offer(universalNode);
- while(!q.isEmpty()){
- int parent = q.poll();
- if(level[parent]>=k){
- continue;//you dont need further infection spread
- }
- for(int child:adjList2[parent]){
- if(visited[child] == 0 && visitable[child]==1 ){
- 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