Karan Shah
Karan Shah

Reputation: 1

Why is my Dijkstra code failing?

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();
        }
    }
}

Upvotes: 0

Views: 522

Answers (1)

peterhzsti
peterhzsti

Reputation: 81

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.

Upvotes: 2

Related Questions