Hi I am solving INCARDS problem using Djikstra Algo. I am continously getting tle.Couldnot find issue.
static Queue<Data> integerPQ;
static int[] currPath;
static int[] distMap;
static int[] visited;
private static int[] dijkstra(int[][] arr, int p) {
currPath = new int[p+1];
distMap = new int[p+1];
visited = new int[p+1];
integerPQ = new PriorityQueue<>();
for(int zz=1; zz<=p; zz++) distMap[zz]= Integer.MAX_VALUE;
integerPQ.add(new Data(1, 0));
while(integerPQ.peek() != null) {
Data dd= integerPQ.poll();
if(distMap[dd.ind] > dd.cost) {
distMap[dd.ind] = dd.cost;
for(int kk= 1; kk<=p; kk++) {
if(arr[dd.ind][kk] != 0) {
int totalPrice= arr[dd.ind][kk] + dd.cost;
if(distMap[kk] > totalPrice && visited[kk] == 0) {
integerPQ.add(new Data(kk, totalPrice));
}
}
}
visited[dd.ind] = 1;
}
}
return distMap;
}
private static class Data implements Comparable<Data>{
int ind;
int cost;
public Data(int ind, int cost){
this.ind= ind;
this.cost= cost;
}
public int compareTo(Data d){
if(d.cost > this.cost)return -1;
else if(d.cost < this.cost) return 1;
else {
if(d.ind > this.ind) return -1;
else if(d.ind < this.ind) return 1;
else return 0;
}
}
}
This is my function returning cost array. How to find out issue?