Hacker Rank : Reading Graph Theory Inputs

by rishul matta

JavaScript

function processData(input) {
    //Enter your code here
    var lines = input.split("\n");
    [nosOfCenteres ,nosOfRoads,typesOfFish] = lines[0].split(" ").map(Number);
     
     var centerToFishMap = {};
     for (var ii = 1; ii <=  nosOfCenteres; ++ii) {
     	centerToFishMap[ii] = lines[ii].split(" ").map(Number).slice(1);
     }
     debugger;
     //dimension of the arrays have to be increased else the indexes start from 0 which is a pain
     var adjacencyMat = new Array(nosOfCenteres + 1);
     var timeReqd, start , end;
      for (; ii - nosOfCenteres <=  nosOfRoads; ++ii) {
     		timeReqd = lines[ii].split(" ").map(Number)[2];
        start = lines[ii].split(" ").map(Number)[0];
         end = lines[ii].split(" ").map(Number)[1];
         
         //arrays have to be created new else they refer to the same instances
         if (!adjacencyMat[start]) {
         	adjacencyMat[start] = []
         }
         
          if (!adjacencyMat[end]) {
         		adjacencyMat[end] = []
         }
         
         adjacencyMat[start][end] = timeReqd;
         adjacencyMat[end][start] = timeReqd;
     }
     
     console.log(adjacencyMat);
} 


processData('5 5 5\n1 1\n1 2\n1 3\n1 4\n1 5\n1 2 10\n1 3 10\n2 4 10\n3 5 10\n4 5 10');



/*
	Input Format

The first line contains  space-separated integers:  (the number of shopping centers),  (the number of roads), and  (the number of types of fish sold in Bitville), respectively.

Each line  of the  subsequent lines () describes a shopping center as a line of space-separated integers. Each line takes the following form:

The first integer, , denotes the number of types of fish that are sold by the fishmonger at the  shopping center.
Each of the  subsequent integers on the line describes a type of fish sold by that fishmonger. Which is denoted by .
Each line  of the  subsequent lines () contains  space-separated integers describing a road. The first two integers,  and , describe the two shopping centers it...