2019-02-06 10:13:55 +08:00
|
|
|
/**
|
|
|
|
* @author Mayank K Jha
|
|
|
|
*/
|
2017-04-24 21:57:05 +08:00
|
|
|
|
2017-10-27 07:56:18 +08:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Scanner;
|
|
|
|
import java.util.Stack;
|
|
|
|
|
2017-12-14 10:57:43 +08:00
|
|
|
public class Dijkshtra {
|
2017-04-24 21:57:05 +08:00
|
|
|
|
2019-02-06 10:13:55 +08:00
|
|
|
public static void main(String[] args) {
|
2018-10-08 21:13:08 +08:00
|
|
|
Scanner in = new Scanner(System.in);
|
|
|
|
|
|
|
|
// n = Number of nodes or vertices
|
2019-02-06 10:13:55 +08:00
|
|
|
int n = in.nextInt();
|
2018-10-08 21:13:08 +08:00
|
|
|
// m = Number of Edges
|
2019-02-06 10:13:55 +08:00
|
|
|
int m = in.nextInt();
|
2018-10-08 21:13:08 +08:00
|
|
|
|
|
|
|
// Adjacency Matrix
|
2019-02-06 10:13:55 +08:00
|
|
|
long[][] w = new long[n + 1][n + 1];
|
2018-10-08 21:13:08 +08:00
|
|
|
|
2019-02-06 10:13:55 +08:00
|
|
|
// Initializing Matrix with Certain Maximum Value for path b/w any two vertices
|
2018-10-08 21:13:08 +08:00
|
|
|
for (long[] row : w) {
|
2019-02-06 10:13:55 +08:00
|
|
|
Arrays.fill(row, 1000000L);
|
2018-10-08 21:13:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* From above,we Have assumed that,initially path b/w any two Pair of vertices is Infinite such that Infinite = 1000000l
|
|
|
|
For simplicity , We can also take path Value = Long.MAX_VALUE , but i have taken Max Value = 1000000l */
|
|
|
|
|
|
|
|
// Taking Input as Edge Location b/w a pair of vertices
|
2019-02-06 10:13:55 +08:00
|
|
|
for (int i = 0; i < m; i++) {
|
|
|
|
int x = in.nextInt(), y = in.nextInt();
|
|
|
|
long cmp = in.nextLong();
|
|
|
|
|
|
|
|
// Comparing previous edge value with current value - Cycle Case
|
|
|
|
if (w[x][y] > cmp) {
|
|
|
|
w[x][y] = cmp;
|
|
|
|
w[y][x] = cmp;
|
|
|
|
}
|
2018-10-08 21:13:08 +08:00
|
|
|
}
|
|
|
|
|
2019-02-06 10:13:55 +08:00
|
|
|
// Implementing Dijkshtra's Algorithm
|
|
|
|
Stack<Integer> t = new Stack<>();
|
2018-10-08 21:13:08 +08:00
|
|
|
int src = in.nextInt();
|
|
|
|
|
2019-02-06 10:13:55 +08:00
|
|
|
for (int i = 1; i <= n; i++) {
|
|
|
|
if (i != src) {
|
2018-10-08 21:13:08 +08:00
|
|
|
t.push(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-06 10:13:55 +08:00
|
|
|
Stack<Integer> p = new Stack<>();
|
2018-10-08 21:13:08 +08:00
|
|
|
p.push(src);
|
|
|
|
w[src][src] = 0;
|
|
|
|
|
2019-02-06 10:13:55 +08:00
|
|
|
while (!t.isEmpty()) {
|
2018-10-08 21:13:08 +08:00
|
|
|
int min = 989997979;
|
|
|
|
int loc = -1;
|
|
|
|
|
2019-02-06 10:13:55 +08:00
|
|
|
for (int i = 0; i < t.size(); i++) {
|
2018-10-08 21:13:08 +08:00
|
|
|
w[src][t.elementAt(i)] = Math.min(w[src][t.elementAt(i)], w[src][p.peek()] + w[p.peek()][t.elementAt(i)]);
|
2019-02-06 10:13:55 +08:00
|
|
|
if (w[src][t.elementAt(i)] <= min) {
|
2018-10-08 21:13:08 +08:00
|
|
|
min = (int) w[src][t.elementAt(i)];
|
|
|
|
loc = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.push(t.elementAt(loc));
|
|
|
|
t.removeElementAt(loc);
|
2019-02-06 10:13:55 +08:00
|
|
|
}
|
2018-10-08 21:13:08 +08:00
|
|
|
|
|
|
|
// Printing shortest path from the given source src
|
2019-02-06 10:13:55 +08:00
|
|
|
for (int i = 1; i <= n; i++) {
|
|
|
|
if (i != src && w[src][i] != 1000000L) {
|
|
|
|
System.out.print(w[src][i] + " ");
|
|
|
|
}
|
|
|
|
// Printing -1 if there is no path b/w given pair of edges
|
|
|
|
else if (i != src) {
|
|
|
|
System.out.print("-1" + " ");
|
|
|
|
}
|
2018-10-08 21:13:08 +08:00
|
|
|
}
|
2017-04-24 21:43:31 +08:00
|
|
|
}
|
2019-02-06 10:13:55 +08:00
|
|
|
}
|