Here’s my solution:
import java.util.PriorityQueue;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
class PriorityQueueNode implements Comparable<PriorityQueueNode> {
private int key;
private int nodeId;
public int getKey() {
return key;
}
public void setKey(int key) {
this.key = key;
}
public int getNodeId() {
return nodeId;
}
public void setNodeId(int nodeId) {
this.nodeId = nodeId;
}
@Override
public int compareTo(PriorityQueueNode o) {
return this.key < o.getKey()? 0: -1;
}
public PriorityQueueNode(int key, int nodeId){
this.key = key;
this.nodeId = nodeId;
}
}
class Graph {
private int[][] AdjacencyMatrix;
public Graph(int nodesCount){
AdjacencyMatrix = new int[nodesCount][];
for(int i = 0; i < AdjacencyMatrix.length; i++){
AdjacencyMatrix[i] = new int[nodesCount];
for(int j = 0; j < nodesCount; j++){
AdjacencyMatrix[i][j] = -1;
}
}
}
public int[][] getAdjacencyMatrix() {
return AdjacencyMatrix;
}
public void createEdge(int node1, int node2, int distance){
AdjacencyMatrix[node1-1][node2-1] = distance;
AdjacencyMatrix[node2-1][node1-1] = distance;
}
public String toString(){
String ans = "";
for(int i = 0; i < AdjacencyMatrix.length; i++){
for(int j = 0; j < AdjacencyMatrix.length; j++){
ans += AdjacencyMatrix[i][j] + " ";
}
ans += "\n";
}
return ans;
}
public Integer getShortestPath(){
//insert all graph's nodes in the heap
//get the min from the heap
//change value of node if it is less than that of its current value
//repeat until the heap is empty
//insert all graph's nodes in the heap
PriorityQueue<PriorityQueueNode> priorityQueue = new PriorityQueue<PriorityQueueNode>();
priorityQueue.add(new PriorityQueueNode(0, 0));
/*for(int i = 1; i < AdjacencyMatrix.length; i++){
priorityQueue.add(new PriorityQueueNode(23665, i));
}*/
int[] ShortestPathArr = new int[AdjacencyMatrix.length];
for(int i = 0; i < AdjacencyMatrix.length; i++){
ShortestPathArr[i] = 23665;
}
ShortestPathArr[0] = 0;
//get the min from the heap
PriorityQueueNode node = null;
while(!priorityQueue.isEmpty()){
node = priorityQueue.peek();
priorityQueue.remove(node);
int nodeId = node.getNodeId();
for(int i = 0; i < AdjacencyMatrix.length; i++){
if(AdjacencyMatrix[nodeId][i] != -1){
if(ShortestPathArr[i] > ShortestPathArr[nodeId] + AdjacencyMatrix[nodeId][i]){
ShortestPathArr[i] = ShortestPathArr[nodeId] + AdjacencyMatrix[nodeId][i];
priorityQueue.add(new PriorityQueueNode(ShortestPathArr[i], i));
}
}
}
}
return ShortestPathArr[ShortestPathArr.length-1];
}
}
class MainClass{
public static void main(String[] args){
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String firstLine = br.readLine();
Graph graph = null;
String[] firstLineArr = firstLine.split(" ");
int citiesCount = Integer.parseInt(firstLineArr[0]);
int edgesCount = Integer.parseInt(firstLineArr[1]);
//create graph as adjacency matrix with no. of cities
graph = new Graph(citiesCount);
for(int i = 0; i < edgesCount; i++){
String currentLine = br.readLine();
String[] currentLineArr = currentLine.split(" ");
int city1 = Integer.parseInt(currentLineArr[0]);
int city2 = Integer.parseInt(currentLineArr[1]);
int distance = Integer.parseInt(currentLineArr[2]);
graph.createEdge(city1, city2, distance);
}
System.out.print(graph.getShortestPath());
} catch(IOException e){
e.printStackTrace();
}
}
}