import java.util.*; public class DijkstraPath { static int[] via; // previous node updating distance public static void main(String[] args) { Scanner sc = new Scanner(System.in); // read from Standard Input int n = sc.nextInt(); // number of nodes int m = sc.nextInt(); // number of edges int[][] mat = new int[n][n]; // Adjacency matrix of node connections for (int i=0; i getRoute(int src, int dst) { ArrayList r = new ArrayList<>(); r.add(dst); int x = dst; while (x != src) { x = via[x]; r.add(x); } Collections.reverse(r); return r; } public static int dijkstra(int[][] mat,int src,int dst) { int n = mat.length; int[] distance = new int[n]; // shortest distance to nodes boolean[] fixed = new boolean[n]; // shortes distance is fixed? via = new int[n]; // previous node for (int i=0; i0 && !fixed[y]) { // if y is directly connected with x and y is unfixed int d2 = distance[x]+mat[x][y];// calculate distance of y via x // if shorter, update the shortest distance of y if (d2 < distance[y]) { distance[y] = d2; via[y] = x; } } } } } // returns index of minimum distance[] whose fixed[] is false static int minIndex(int[] distance,boolean[] fixed) { int idx = -1; for (int i=0; i distance[i]) idx = i; // index of shortest node } return idx; } }