We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
The following is the code I have so far, I just am having trouble with generating the adjacency matrix via DFS. Could you help me out here? Thanks.
importjava.io.*;importjava.util.*;importjava.text.*;importjava.math.*;importjava.util.regex.*;publicclassSolution{privatestaticinttotal;privatestaticboolean[]visited;privatestaticint[][]adjCities;publicstaticvoidmain(String[]args){Scannerin=newScanner(System.in);intq=in.nextInt();// number of queriesfor(inta0=0;a0<q;a0++){intcities=in.nextInt();// number of citiesintroads=in.nextInt();// number of roadsintlibCost=in.nextInt();// cost of building a library//System.out.println("Cost of building library: "+libCost);introadCost=in.nextInt();// cost of repairing a road//System.out.println("Cost of repairing road: "+roadCost);if(roadCost>=libCost){// optimal scenariototal=libCost*cities;}else{visited=newboolean[cities];adjCities=newint[cities][cities];//initializing adjacency matrixfor(inti=0;i<roads;i++){intc1=in.nextInt();intc2=in.nextInt();adjCities[c1-1][c1-1]=c1;adjCities[c2-1][c2-1]=c2;dfs(c1);}}System.out.println(total);}}privatestaticvoiddfs(intcity){visited[city]=true;for(intc:adjCities[city]){if(!visited[c]){//total += roadCost;dfs(c);}}}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Roads and Libraries
You are viewing a single comment's thread. Return to all comments →
The following is the code I have so far, I just am having trouble with generating the adjacency matrix via DFS. Could you help me out here? Thanks.