Why am I getting wrong answer in Dijkstras algorithm problem?

import java.util.Scanner;
import java.util.*;
public class Main {
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int f=0;f<t;f++){
Graph g=new Graph();
int n=sc.nextInt();
int m=sc.nextInt();
for(int i=1;i<=n;i++){
g.addVertex(i+"");
}
for(int i=0;i<m;i++){
int x=sc.nextInt();
int y=sc.nextInt();
int r=sc.nextInt();
g.addEdge(x+"",y+"",r);
}

        g.Dijkstra(sc.nextInt()+"");
    }
}

}
class Graph{
HashMap<String,Vertex> vtces=new HashMap<>();
private class Vertex{
String vname;
HashMap<String,Integer> nbrs;
Vertex(String vname){
this.vname=vname;
nbrs=new HashMap<String,Integer>();
}
}

// add an Vertex

public void addVertex(String vname){
    Vertex vtx=new Vertex(vname);
    this.vtces.put(vname,vtx);
}

//Return number of vertices.

public int numVertex(){
    return this.vtces.size();
}

//Contains vertex

public boolean containsVertex(String vertex){
    return this.vtces.containsKey(vertex);
}

// Add an Edge

public void addEdge(String vname1,String vname2,int cost){
    Vertex vtx=vtces.get(vname1);
    vtx.nbrs.put(vname2,cost);
    vtx=vtces.get(vname2);
    vtx.nbrs.put(vname1,cost);
}

// Return number of Edges

public int numEdges(){
    Set<String> set=vtces.keySet();
    int sum=0;
    for(String str:set){
        sum+=vtces.get(str).nbrs.size();
    }
    return sum/2;
}

// PRIVATE CLASS PAIRS
private class DijkstraPair implements Comparable<DijkstraPair>{
    String vname;
    String acqvname;
    int cost;
    DijkstraPair(String vname){
        this.vname=vname;
        acqvname=null;
        cost=Integer.MAX_VALUE;
    }
    public int compareTo(DijkstraPair o){
        return o.cost-this.cost;
    }
}
public int getEdge(String str1,String str2){
    if(str1.equals(str2))
        return 0;
    else
        return vtces.get(str1).nbrs.get(str2);
}
public void Dijkstra(String src){
    HashMap<String,Integer> ans=new HashMap<>();
    Set<String> set=vtces.keySet();
    HeapGeneric<DijkstraPair> heap=new HeapGeneric<>();
    HashMap<String,DijkstraPair> track=new HashMap<>();
    for(String key:set){

        DijkstraPair dp=new DijkstraPair(key);
        heap.add(dp);
        track.put(key,dp);
    }
    track.get(src).cost=0;
    HashMap<String,Integer> visited=new HashMap<>();
   // DijkstraPair prev;
    int prev=0;
    while(!heap.isEmpty()){
        DijkstraPair dp=heap.remove();
        visited.put(dp.vname,1);
        prev=dp.cost;
        if(prev==Integer.MAX_VALUE)
            ans.put(dp.vname,-1);
        else
        ans.put(dp.vname,prev);
        //System.out.println(src+"-"+dp.vname+":"+prev);
        set=vtces.get(dp.vname).nbrs.keySet();
        for(String str:set){
            if(!visited.containsKey(str)){
                int newdist=getEdge(str,dp.vname);
                int old=track.get(str).cost;
                if(prev+newdist<old){
                    track.get(str).cost=prev+newdist;
                    heap.updatePriority(track.get(str));
                }
            }
        }

    }
    set=ans.keySet();
    for(String str:set){
        if(!str.equals(src))
        System.out.print(ans.get(str)+" ");
    }
    System.out.println();
}

}
class HeapGeneric<T extends Comparable> {
ArrayList data=new ArrayList();
HashMap<T,Integer> Index=new HashMap<>();
// Add an Element.

public void add(T item){
    data.add(item);
    Index.put(item,this.data.size()-1);
    upheapify(data.size()-1);
}

public int size(){
    return data.size();
}

public void upheapify(int ci){
    int pi=(ci-1)/2;
    if((isLarger(data.get(ci),data.get(pi)))>0){
        swap(pi,ci);
        upheapify(pi);
    }
    else
        return;
}
public void  swap(int i,int j){
    T ith=data.get(i);
    T jth=data.get(j);
    data.set(i,jth);
    data.set(j,ith);

    Index.put(ith,j);
    Index.put(jth,i);
}

public T remove(){
    swap(0,size()-1);
    T rv=data.remove(size()-1);
    Index.remove(rv);
    downheapify(0);
    return rv;
}
public boolean isEmpty(){
    return (this.data.size()==0);
}
public void downheapify(int pi){
    int mini=pi;
    int lci=pi*2+1;
    int rci=pi*2+2;
    if(lci<size() && isLarger(data.get(lci),data.get(mini))>0){
        mini=lci;
    }
    if(rci<size() && isLarger(data.get(rci),data.get(mini))>0){
        mini=rci;
    }
    if(mini!=pi){
        swap(pi,mini);
        downheapify(mini);
    }
}
public T get(){
    return this.data.get(0);
}
public int isLarger(T t,T o){
    return t.compareTo(o);
}

public void updatePriority(T pair){
    int ind=Index.get(pair);
    upheapify(ind);
}

}

Please tell the error in my code for which testcases it is not working??

@Anubhav44044,

Test case:
1
20 54
1 7 45
2 14 15
3 7 29
4 1 48
5 1 66
6 7 17
7 14 15
8 14 43
9 1 27
10 1 33
11 14 64
12 14 27
13 7 66
14 7 54
15 14 56
16 7 21
17 1 20
18 1 34
19 7 52
20 14 14
9 14 9
15 1 39
12 1 24
9 1 16
1 2 33
18 1 46
9 1 28
15 14 3
12 1 27
1 2 5
15 1 34
1 2 28
9 7 16
3 7 23
9 7 21
9 14 19
3 1 20
3 1 5
12 14 19
3 14 2
12 1 46
3 14 5
9 14 44
6 14 26
9 14 16
9 14 34
6 7 42
3 14 27
1 7 9
1 7 41
15 14 19
12 7 13
3 7 10
1 7 2
17

Correct output: 20 25 25 68 86 39 22 70 36 53 91 35 88 27 30 43 54 74 41
Your output: 0 0 0 -2147483635 0 0 0 0 -2147483644 0 0 0 0 0 -2147483642 0 0 0 0

@Anubhav44044,
You can use the above test case to debug your code. If you still face any problems, reply to me on this thread. I will be happy to help you

import java.util.*;
public class Main {
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int f=0;f<t;f++){
Graph g=new Graph();
int n=sc.nextInt();
int m=sc.nextInt();
for(int i=1;i<=n;i++){

            g.addVertex(i+"");
        }
        for(int i=0;i<m;i++){
            int x=sc.nextInt();
            int y=sc.nextInt();
            int r=sc.nextInt();
            g.addEdge(x+"",y+"",r);
        }

        g.Dijkstra(sc.nextInt()+"");
    }
}

}
class HeapGeneric<T extends Comparable> {
ArrayList data=new ArrayList();
HashMap<T,Integer> Index=new HashMap<>();
// Add an Element.

public void add(T item){
    data.add(item);
    Index.put(item,this.data.size()-1);
    upheapify(data.size()-1);
}

public int size(){
    return data.size();
}

public void upheapify(int ci){
    int pi=(ci-1)/2;
    if((isLarger(data.get(ci),data.get(pi)))>0){
        swap(pi,ci);
        upheapify(pi);
    }
    else
        return;
}
public void  swap(int i,int j){
    T ith=data.get(i);
    T jth=data.get(j);
    data.set(i,jth);
    data.set(j,ith);

    Index.put(ith,j);
    Index.put(jth,i);
}

public T remove(){
    swap(0,size()-1);
    T rv=data.remove(size()-1);
    Index.remove(rv);
    downheapify(0);
    return rv;
}
public boolean isEmpty(){
    return (this.data.size()==0);
}
public void downheapify(int pi){
    int mini=pi;
    int lci=pi*2+1;
    int rci=pi*2+2;
    if(lci<size() && isLarger(data.get(lci),data.get(mini))>0){
        mini=lci;
    }
    if(rci<size() && isLarger(data.get(rci),data.get(mini))>0){
        mini=rci;
    }
    if(mini!=pi){
        swap(pi,mini);
        downheapify(mini);
    }
}
public T get(){
    return this.data.get(0);
}
public int isLarger(T t,T o){
    return t.compareTo(o);
}

public void updatePriority(T pair){
    int ind=Index.get(pair);
    upheapify(ind);
}

}
class Graph {
HashMap<String,Vertex> vtces=new HashMap<>();
private class Vertex{
String vname;
HashMap<String,Integer> nbrs;
Vertex(String vname){
this.vname=vname;
nbrs=new HashMap<String,Integer>();
}
}

// add an Vertex

public void addVertex(String vname){
    Vertex vtx=new Vertex(vname);
    this.vtces.put(vname,vtx);
}

//Return number of vertices.

public int numVertex(){
    return this.vtces.size();
}

//Contains vertex

public boolean containsVertex(String vertex){
    return this.vtces.containsKey(vertex);
}

// Add an Edge

public void addEdge(String vname1,String vname2,int cost){
    Vertex vtx1=this.vtces.get(vname1);
    Vertex vtx2=this.vtces.get(vname2);
    if(vtx1!=null && vtx2!=null) {
        if (vtx1.nbrs.containsKey(vname2) && vtx2.nbrs.containsKey(vname1)) {
            if(vtx1.nbrs.get(vname2)>cost){
                vtx1.nbrs.put(vname2,cost);
                vtx2.nbrs.put(vname1,cost);
            }
            else
            {

            }
        }
        else{
            vtx1.nbrs.put(vname2,cost);
            vtx2.nbrs.put(vname1,cost);
        }
    }
}

// Return number of Edges

public int numEdges(){
    Set<String> set=vtces.keySet();
    int sum=0;
    for(String str:set){
        sum+=vtces.get(str).nbrs.size();
    }
    return sum/2;
}

// PRIVATE CLASS PAIRS
private class DijkstraPair implements Comparable<DijkstraPair>{
    String vname;
    String acqvname;
    int cost;
    DijkstraPair(String vname){
        this.vname=vname;
        acqvname=null;
        cost=Integer.MAX_VALUE;
    }
    public int compareTo(DijkstraPair o){
        return o.cost-this.cost;
    }
}
public int getEdge(String str1,String str2){
    if(str1.equals(str2) || !vtces.get(str1).nbrs.containsKey(str2))
        return 0;
    else
        return vtces.get(str1).nbrs.get(str2);
}
public void Dijkstra(String src){
    HashMap<String,Integer> ans=new HashMap<>();
    Set<String> set=vtces.keySet();
    HeapGeneric<DijkstraPair> heap=new HeapGeneric<>();
    HashMap<String,DijkstraPair> track=new HashMap<>();
    for(String key:set){

        DijkstraPair dp=new DijkstraPair(key);
        heap.add(dp);
        track.put(key,dp);
    }
    track.get(src).cost=0;
    heap.updatePriority(track.get(src));
    HashMap<String,Integer> visited=new HashMap<>();

    int prev=0;
    while(!heap.isEmpty()){
        DijkstraPair dp=heap.remove();
        visited.put(dp.vname,1);
        prev=dp.cost;

        if(prev>=Integer.MAX_VALUE)
            prev=-1;
        ans.put(dp.vname,prev);


        set=vtces.get(dp.vname).nbrs.keySet();
        for(String str:set){
            if(!visited.containsKey(str)){
                int newdist=prev+getEdge(str,dp.vname);
                int old=track.get(str).cost;
                if(newdist<old){
                    track.get(str).cost=newdist;
                    heap.updatePriority(track.get(str));
                    //System.out.print(str+" : "+newdist+",");
                }

            }
        }
       // System.out.println();

    }
    set=ans.keySet();
    //System.out.println(ans);
    int a[]=new int[vtces.size()+1];
    for(String str:set){
       int b=Integer.parseInt(str);
       a[b]=ans.get(str);
    }
    int j=Integer.parseInt(src);

    for(int i=1;i<a.length;i++){
        if(i!=j)
        System.out.print(a[i]+" ");
    }
    System.out.println();
}

}

This Code is giving wrong answer but is working completely fine for the above test case also.

@Anubhav44044,
But for the test given below its giving wrong answer:
1
4 4
1 2 24
1 4 20
3 1 3
4 3 12
1

No it giving the right answer for this test case which is "24 3 15 ".

This is the only desired output!!!

please tell the mistake in my code

import java.util.*;
public class Main {
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int f=0;f<t;f++){
Graph g=new Graph();
int n=sc.nextInt();
int m=sc.nextInt();
for(int i=1;i<=n;i++){

            g.addVertex(i+"");
        }
        for(int i=0;i<m;i++){
            int x=sc.nextInt();
            int y=sc.nextInt();
            int r=sc.nextInt();
            g.addEdge(x+"",y+"",r);
        }

        g.Dijkstra(sc.nextInt()+"");
    }
}

}
class HeapGeneric<T extends Comparable> {
ArrayList data=new ArrayList();
HashMap<T,Integer> Index=new HashMap<>();
// Add an Element.

public void add(T item){
    data.add(item);
    Index.put(item,this.data.size()-1);
    upheapify(data.size()-1);
}

public int size(){
    return data.size();
}

public void upheapify(int ci){
    int pi=(ci-1)/2;
    if((isLarger(data.get(ci),data.get(pi)))>0){
        swap(pi,ci);
        upheapify(pi);
    }
    else
        return;
}
public void  swap(int i,int j){
    T ith=data.get(i);
    T jth=data.get(j);
    data.set(i,jth);
    data.set(j,ith);

    Index.put(ith,j);
    Index.put(jth,i);
}

public T remove(){
    swap(0,size()-1);
    T rv=data.remove(size()-1);
    Index.remove(rv);
    downheapify(0);
    return rv;
}
public boolean isEmpty(){
    return (this.data.size()==0);
}
public void downheapify(int pi){
    int mini=pi;
    int lci=pi*2+1;
    int rci=pi*2+2;
    if(lci<size() && isLarger(data.get(lci),data.get(mini))>0){
        mini=lci;
    }
    if(rci<size() && isLarger(data.get(rci),data.get(mini))>0){
        mini=rci;
    }
    if(mini!=pi){
        swap(pi,mini);
        downheapify(mini);
    }
}
public T get(){
    return this.data.get(0);
}
public int isLarger(T t,T o){
    return t.compareTo(o);
}

public void updatePriority(T pair){
    int ind=Index.get(pair);
    upheapify(ind);
}

}
class Graph {
HashMap<String,Vertex> vtces=new HashMap<>();
private class Vertex{
String vname;
HashMap<String,Integer> nbrs;
Vertex(String vname){
this.vname=vname;
nbrs=new HashMap<String,Integer>();
}
}

// add an Vertex

public void addVertex(String vname){
    Vertex vtx=new Vertex(vname);
    this.vtces.put(vname,vtx);
}

//Return number of vertices.

public int numVertex(){
    return this.vtces.size();
}

//Contains vertex

public boolean containsVertex(String vertex){
    return this.vtces.containsKey(vertex);
}

// Add an Edge

public void addEdge(String vname1,String vname2,int cost){
    Vertex vtx1=this.vtces.get(vname1);
    Vertex vtx2=this.vtces.get(vname2);
    if(vtx1!=null && vtx2!=null) {
        if (vtx1.nbrs.containsKey(vname2) && vtx2.nbrs.containsKey(vname1)) {
            if(vtx1.nbrs.get(vname2)>cost){
                vtx1.nbrs.put(vname2,cost);
                vtx2.nbrs.put(vname1,cost);
            }
            else
            {

            }
        }
        else{
            vtx1.nbrs.put(vname2,cost);
            vtx2.nbrs.put(vname1,cost);
        }
    }
}

// Return number of Edges

public int numEdges(){
    Set<String> set=vtces.keySet();
    int sum=0;
    for(String str:set){
        sum+=vtces.get(str).nbrs.size();
    }
    return sum/2;
}

// PRIVATE CLASS PAIRS
private class DijkstraPair implements Comparable<DijkstraPair>{
    String vname;
    String acqvname;
    int cost;
    DijkstraPair(String vname){
        this.vname=vname;
        acqvname=null;
        cost=Integer.MAX_VALUE;
    }
    public int compareTo(DijkstraPair o){
        return o.cost-this.cost;
    }
}
public int getEdge(String str1,String str2){
    if(str1.equals(str2) || !vtces.get(str1).nbrs.containsKey(str2))
        return 0;
    else
        return vtces.get(str1).nbrs.get(str2);
}
public void Dijkstra(String src){
    HashMap<String,Integer> ans=new HashMap<>();
    Set<String> set=vtces.keySet();
    HeapGeneric<DijkstraPair> heap=new HeapGeneric<>();
    HashMap<String,DijkstraPair> track=new HashMap<>();
    for(String key:set){

        DijkstraPair dp=new DijkstraPair(key);
        heap.add(dp);
        track.put(key,dp);
    }
    track.get(src).cost=0;
    heap.updatePriority(track.get(src));
    HashMap<String,Integer> visited=new HashMap<>();

    int prev=0;
    while(!heap.isEmpty()){
        DijkstraPair dp=heap.remove();
        visited.put(dp.vname,1);
        prev=dp.cost;

        if(prev>=Integer.MAX_VALUE)
            prev=-1;
        ans.put(dp.vname,prev);


        set=vtces.get(dp.vname).nbrs.keySet();
        for(String str:set){
            if(!visited.containsKey(str)){
                int newdist=prev+getEdge(str,dp.vname);
                int old=track.get(str).cost;
                if(newdist<old){
                    track.get(str).cost=newdist;
                    heap.updatePriority(track.get(str));
                    //System.out.print(str+" : "+newdist+",");
                }

            }
        }
       // System.out.println();

    }
    set=ans.keySet();
    //System.out.println(ans);
    int a[]=new int[vtces.size()+1];
    for(String str:set){
       int b=Integer.parseInt(str);
       a[b]=ans.get(str);
    }
    int j=Integer.parseInt(src);

    for(int i=1;i<a.length;i++){
        if(i!=j)
        System.out.print(a[i]+" ");
    }
    System.out.println();
}

}

@Anubhav44044,
Hey I am really sorry for the late response. I went through your code several times but I couldn’t keep a track of the changes. I have attached the test case which is giving error. Please try your code for this.

Input:

1
94 95
1 19 56
2 57 41
3 1 32
4 1 36
5 1 24
6 19 16
7 19 25
8 76 6
9 76 23
10 19 25
11 76 64
12 19 6
13 76 12
14 76 18
15 76 47
16 1 53
17 19 36
18 19 25
19 38 55
20 1 1
21 76 57
22 1 44
23 57 19
24 38 15
25 1 16
26 19 37
27 76 22
28 57 54
29 19 47
30 76 15
31 57 5
32 19 13
33 19 65
34 1 19
35 19 17
36 38 12
37 1 5
38 19 23
39 76 33
40 38 19
41 19 4
42 76 32
43 57 27
44 57 18
45 1 13
46 19 40
47 1 62
48 76 46
49 1 18
50 19 4
51 1 58
52 1 9
53 19 5
54 57 63
55 76 29
56 1 28
57 38 26
58 19 47
59 57 43
60 76 37
61 76 14
62 1 40
63 76 27
64 1 28
65 76 32
66 1 47
67 38 21
68 76 58
69 38 10
70 38 46
71 57 59
72 1 7
73 38 55
74 57 51
75 38 45
76 77 9
77 76 46
78 38 43
79 1 61
80 57 43
81 1 2
82 19 64
83 1 9
84 1 14
85 57 49
86 57 22
87 19 12
88 76 55
89 76 26
90 1 36
91 1 63
92 57 51
93 19 7
94 1 24
55 76 9
85

Correct Output:

154 90 186 190 178 114 123 -1 -1 123 -1 104 -1 -1 -1 207 134 123 98 155 -1 198 68 90 170 135 -1 103 145 -1 54 111 163 173 115 87 159 75 -1 94 102 -1 76 67 167 138 216 -1 172 102 212 163 103 112 -1 182 49 145 92 -1 -1 194 -1 182 -1 201 96 -1 85 121 108 161 130 100 120 -1 -1 118 215 92 156 162 163 168 71 110 -1 -1 190 217 100 105 178

The error is that instead of -1 your code is printing a distance.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.