Dijkstra shortest reach 2 hackerrank solution
Hackerrank - Dijkstra Shortest Reach 2 I got stuck at TestCase 7 (the only one I failed) which I thought was my fault. I downloaded the test case and checked against my output generated. I do git diff and I can't see any difference between them. Can you help me verify what happened to my code? Or perhaps if there isn't a catch in my code, I would like to alter the question: Does HackerRank Platform often has bugs? I often encountered an obscure failure (usually last one or two out of 13 testcases) when doing a HackerRank Challenge for my job interview, hence failed multiple times at these. I wonder if any of you has similar experience. My suspicion is that when I checked my submitted code with friends, we can't find any edge cases or bugs. It should be perfect. As programmer who had been coding in LeetCode, this terrifies me and I started to train on HackerRank. Please advise. Thanks Resource: P.S in the google drive folder, I attached my output: output_me.txt and the ground truth output: output.txt. I did add new lines for both output (initially, all answer are in one long line, added new lines to make it easier to read.) Code: Regards, Me HackerRank Solution: Dijkstra Shortest Reach 2
Published on: 25th May 2018 This tutorial provides Java solution to "Dijkstra: Shortest Reach 2" challenge of HackerRank. Given a graph consisting N nodes (labelled 1 to N) where a specific given node S represents the starting position S and an edge between two nodes is of a given length, which may or may not be equal to other lengths in the graph. It is required to calculate the shortest distance from the start position (Node S) to all of the other nodes in the graph. Note: If a node is unreachable , the distance is assumed as -1. Input Format:The first line contains T, denoting the number of test cases. The next M lines each consist of three space-separated integers x y r, where x and y denote the two nodes between which the undirected edge exists, r denotes the length of edge between these corresponding nodes. The last line has an integer S, denoting the starting position. Output Format:For each of the T test cases, print a single line consisting N - 1 space separated integers denoting the shortest distance of N - 1 nodes other than S from starting position S in increasing order of their labels. For unreachable nodes, print -1. Constraints:
If there are edges between the same pair of nodes with different weights, they are to be considered as is, like multiple edges. Sample Input:1 4 4 1 2 24 1 4 20 3 1 3 4 3 12 1Sample Output:24 3 15Explanation:The graph given in the test case is shown as :
The shortest paths followed for the three nodes B,C and D are as follows : S->B - Shortest Path Value : 24 S->C - Shortest Path Value : 3 S->C->D - Shortest Path Value : 15 We will be using adjacency lists for modeling the nodes of our graph. In this model, each of the node will have list of nodes it is linked to along with the distance. Here is how our Node structure will look like with id, distance from source Node and linkedNodes- /** * Represents Node of the graph and contains node id along with the nodes and distance that are attached to it. */ private static class Node implements ComparableWe also have added hashCode, equals and compareTo method as we will be using PriorityQueue to keep distance for each node. In order to do this, we define equality based on id so that we don't add two records with same id. However, we want to sort the Nodes on the basis of their distances so we use distance field in compareTo method. Now here is the complete Java implemenation of this algorithm - package com.saintech.allprogtutorials.hackerrank.algos; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.PriorityQueue; import java.util.Scanner; /** * @author Sain Technology Solutions * * Solution to Problem - https://www.hackerrank.com/challenges/dijkstrashortreach * */ public class DijkstraShortestReach2 { /** * Represents Node of the graph and contains node id along with the nodes and distance that are attached to it. */ private static class Node implements ComparableThank you for reading through the tutorial. In case of any feedback/questions/concerns, you can communicate same to us through your comments and we shall get back to you as soon as possible.
Published on: 25th May 2018 |