0

I am trying to solve a hackerrank question on Dijkstra's Algorithm-https://www.hackerrank.com/challenges/dijkstrashortreach. I am using kind of my own logic of Dijkstra's code. Although my code solves the easier test cases, it fails at higher ones. I am guessing my code is missing some transitivity somewhere and I am getting higher than expected values for some node. Can you please help me spot my error? Question: Input Format The first line contains T, denoting the number of test cases. First line of each test case has two integers N, denoting the number of nodes in the graph and M, denoting the number of edges in the graph. The next 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. If there are edges between the same pair of nodes with different weights, they are to be considered as is, like multiple edges.

Output Format For each of the test cases, print a single line consisting N-1 space separated integers denoting the shortest distance of N-1 nodes from starting position S. For unreachable nodes, print -1

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int number = sc.nextInt();
        for (int i = 0; i < number; i++) {
            int n = sc.nextInt();
            n++;
            int m = sc.nextInt();
            int mat[][] = new int[n][n];
            for (int v = 0; v < m; v++) {
                int r = sc.nextInt();
                int c = sc.nextInt();
                int weight = sc.nextInt();
                mat[r][c] = weight;
                mat[c][r] = weight;
            }

            int dist[] = new int[n];
            for (int bb = 0; bb < n; bb++) { 
                dist[bb] = Integer.MAX_VALUE;
            }
            Queue<Integer> q = new LinkedList<Integer>();
            int source = sc.nextInt();
            q.add(source);
            dist[source] = 0;
            while (!q.isEmpty()) {
                int g = q.remove();
                for (int k = 1; k < n; k++) {
                    if (mat[g][k] > 0)  { // if mat[g][k]==0, it means there is no edge between the nodes and hence they are not neighbours.
                        if (g == k)
                            continue;
                        if (dist[k] >= dist[g] + mat[k][g]) {
                            dist[k] = dist[g] + mat[k][g];
                            q.add(k);               
                        }
                    }
                }
            }
            for (int f = 1; f < n; f++) {
                if (f == source)
                    continue;
                if (dist[f] == Integer.MAX_VALUE)
                    System.out.print("-1" + " ");
                else
                    System.out.print(dist[f] + " ");
            }
            System.out.println();
        }
    }
}
  • 1
    please show input data and the desired output format – JimHawkins Jun 17 '16 at 10:08
  • I was going to. Unfortunately, the engine mistook it for code and incessantly asked me to indent it. Please visit the link mentioned above for the entire problem including the I/O formats. https://www.hackerrank.com/challenges/dijkstrashortreach – Karan Shah Jun 18 '16 at 08:06

1 Answers1

2

At first glance, you said there can be multiple edges, but as far I can see you store only one, always the latest in the input: mat[r][c] = weight; This (and the next line ofc) just overwrites the maybe already existing and less weighted edge. You should store the minimum weighted edge between two nodes.

peterhzsti
  • 71
  • 2
  • The next line is done so as to ensure that the matrix is symmetric. The constraint is that the number of edges are n*(n-1)/2. This ensures that there is only one edge between both the nodes. Hence, I am not overwriting anything. Its a undirected graph, by the way. Feel free to ask any question regarding the logic or any other doubt. Thanks for your help :) – Karan Shah Jun 18 '16 at 08:12
  • Could you give an example input which for your program fails, or the site just says it's wrong? One suggestion (it doesn't solve the problem) in this line: "if (dist[k] >= dist[g] + mat[k][g]) {" the strict > is enough. If they are equal, then won't find you a better solution, the retesting of the node will be redundant. – peterhzsti Jun 21 '16 at 10:52
  • The code does solve the smaller base cases. But it fails at higher test cases. The input is- https://hr-testcases-us-east-1.s3.amazonaws.com/5595/input01.txt?AWSAccessKeyId=AKIAJAMR4KJHHUS76CYQ&Expires=1466673358&Signature=%2Fn07OlJVBOPgn6Zs9MahuheRZr8%3D&response-content-type=text%2Fplain The Expected output is-https://hr-testcases-us-east-1.s3.amazonaws.com/5595/output01.txt?AWSAccessKeyId=AKIAJAMR4KJHHUS76CYQ&Expires=1466673488&Signature=q5reYURfG%2FI2l31LO5MPnh%2FVUfs%3D&response-content-type=text%2Fplain The output can be received by running the code in your computer. 2 long to post it – Karan Shah Jun 23 '16 at 07:26