MY CODE IS PASSING THE SAMPLE TEST CASE AND MANY OTHER CASES BUT ON SUBMISSION, IT IS SHOWING ‘RUN ERROR’.
I AM POSTING ONLY THE CONCERNED FUNCTION. REST ALL THE CLASSES AND FUNCTIONS USED ARE WORKING FINE. PLEASE TELL ME WHERE I AM DOING MISTAKE.
public HashMap<Integer,Integer> dijkstra(int src,Graph graph)
{
HashMap<Integer,Integer> ans=new HashMap<>();
HashMap<Integer,DijkstraPair> map=new HashMap<>();
HeapGeneric heap=new HeapGeneric<>();
for(Integer key:graph.vtces.keySet())
{
DijkstraPair np=new DijkstraPair();
np.vname=key;
np.psf="";
np.cost=Integer.MAX_VALUE;
if(key.equals(src))
{
np.cost=0;
np.psf=key+"";
}
heap.add(np);
map.put(key, np);
}
HashMap<Integer,Boolean> updated=new HashMap<>();
while(!heap.isEmpty())
{
DijkstraPair rp=heap.remove();
map.remove(rp.vname);
ans.put(rp.vname,rp.cost);
int flag=0;
for(Integer nbr:graph.vtces.get(rp.vname).nbrs.keySet())
{
if(map.containsKey(nbr))
{
flag++;
int oc=map.get(nbr).cost;
int nc=rp.cost+graph.vtces.get(rp.vname).nbrs.get(nbr);
if(oc>nc)
{
DijkstraPair gp=map.get(nbr);
gp.psf=rp.psf+nbr;
gp.cost=nc;
updated.put(nbr,true);
heap.updatePriority(gp);
}
}
}
if(flag==0)
{
ArrayList keys=new ArrayList<>(map.keySet());
for(Integer key:keys)
{
if(!updated.containsKey(key))
{
ans.put(key,-1);
map.get(key).cost=-1;
heap.remove();
}
}
}
}
return ans;
}