|
3 | 3 |
|
4 | 4 | */ |
5 | 5 |
|
6 | | - |
7 | 6 | import java.io.IOException; |
8 | 7 | import java.util.Arrays; |
9 | 8 | import java.util.Scanner; |
10 | 9 | import java.util.Stack; |
11 | 10 |
|
12 | 11 | public class Dijkshtra { |
13 | 12 |
|
14 | | -public static void main(String[] args) throws IOException { |
15 | | - Scanner in =new Scanner(System.in); |
16 | | - |
17 | | - int n=in.nextInt(); //n = Number of nodes or vertices |
18 | | - int m=in.nextInt(); //m = Number of Edges |
19 | | - long w[][]=new long [n+1][n+1]; //Adjacency Matrix |
20 | | - |
21 | | - //Initializing Matrix with Certain Maximum Value for path b/w any two vertices |
22 | | - for (long[] row: w) |
23 | | - Arrays.fill(row, 1000000l); |
24 | | - //From above,we Have assumed that,initially path b/w any two Pair of vertices is Infinite such that Infinite = 1000000l |
25 | | - //For simplicity , We can also take path Value = Long.MAX_VALUE , but i have taken Max Value = 1000000l . |
26 | | - |
27 | | - //Taking Input as Edge Location b/w a pair of vertices |
28 | | - for(int i=0;i<m;i++){ |
29 | | - int x=in.nextInt(),y=in.nextInt(); |
30 | | - long cmp=in.nextLong(); |
31 | | - if(w[x][y]>cmp){ //Comparing previous edge value with current value - Cycle Case |
32 | | - w[x][y]=cmp; w[y][x]=cmp; |
33 | | - } |
| 13 | + public static void main(String[] args) throws IOException { |
| 14 | + Scanner in = new Scanner(System.in); |
| 15 | + |
| 16 | + // n = Number of nodes or vertices |
| 17 | + int n = in.nextInt(); |
| 18 | + // m = Number of Edges |
| 19 | + int m = in.nextInt(); |
| 20 | + |
| 21 | + // Adjacency Matrix |
| 22 | + long w[][] = new long [n+1][n+1]; |
| 23 | + |
| 24 | + //Initializing Matrix with Certain Maximum Value for path b/w any two vertices |
| 25 | + for (long[] row : w) { |
| 26 | + Arrays.fill(row, 1000000l); |
| 27 | + } |
| 28 | + |
| 29 | + /* From above,we Have assumed that,initially path b/w any two Pair of vertices is Infinite such that Infinite = 1000000l |
| 30 | + For simplicity , We can also take path Value = Long.MAX_VALUE , but i have taken Max Value = 1000000l */ |
| 31 | + |
| 32 | + // Taking Input as Edge Location b/w a pair of vertices |
| 33 | + for(int i = 0; i < m; i++) { |
| 34 | + int x = in.nextInt(),y=in.nextInt(); |
| 35 | + long cmp = in.nextLong(); |
| 36 | + |
| 37 | + //Comparing previous edge value with current value - Cycle Case |
| 38 | + if(w[x][y] > cmp) { |
| 39 | + w[x][y] = cmp; w[y][x] = cmp; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // Implementing Dijkshtra's Algorithm |
| 44 | + Stack<Integer> t = new Stack<Integer>(); |
| 45 | + int src = in.nextInt(); |
| 46 | + |
| 47 | + for(int i = 1; i <= n; i++) { |
| 48 | + if(i != src) { |
| 49 | + t.push(i); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + Stack <Integer> p = new Stack<Integer>(); |
| 54 | + p.push(src); |
| 55 | + w[src][src] = 0; |
| 56 | + |
| 57 | + while(!t.isEmpty()) { |
| 58 | + int min = 989997979; |
| 59 | + int loc = -1; |
| 60 | + |
| 61 | + for(int i = 0; i < t.size(); i++) { |
| 62 | + w[src][t.elementAt(i)] = Math.min(w[src][t.elementAt(i)], w[src][p.peek()] + w[p.peek()][t.elementAt(i)]); |
| 63 | + if(w[src][t.elementAt(i)] <= min) { |
| 64 | + min = (int) w[src][t.elementAt(i)]; |
| 65 | + loc = i; |
| 66 | + } |
| 67 | + } |
| 68 | + p.push(t.elementAt(loc)); |
| 69 | + t.removeElementAt(loc); |
| 70 | + } |
| 71 | + |
| 72 | + // Printing shortest path from the given source src |
| 73 | + for(int i = 1; i <= n; i++) { |
| 74 | + if(i != src && w[src][i] != 1000000l) { |
| 75 | + System.out.print(w[src][i] + " "); |
| 76 | + } |
| 77 | + // Printing -1 if there is no path b/w given pair of edges |
| 78 | + else if(i != src) { |
| 79 | + System.out.print("-1" + " "); |
| 80 | + } |
| 81 | + } |
34 | 82 | } |
35 | | - |
36 | | - //Implementing Dijkshtra's Algorithm |
37 | | - |
38 | | - Stack<Integer> t=new Stack<Integer>(); |
39 | | - int src=in.nextInt(); |
40 | | - for(int i=1;i<=n;i++){ |
41 | | - if(i!=src){t.push(i);}} |
42 | | - Stack <Integer> p=new Stack<Integer>(); |
43 | | - p.push(src); |
44 | | - w[src][src]=0; |
45 | | - while(!t.isEmpty()){int min=989997979,loc=-1; |
46 | | - for(int i=0;i<t.size();i++){ |
47 | | - w[src][t.elementAt(i)]=Math.min(w[src][t.elementAt(i)],w[src][p.peek()] |
48 | | - +w[p.peek()][t.elementAt(i)]); |
49 | | - if(w[src][t.elementAt(i)]<=min){ |
50 | | - min=(int) w[src][t.elementAt(i)];loc=i;} |
51 | | - } |
52 | | - p.push(t.elementAt(loc));t.removeElementAt(loc);} |
53 | | - |
54 | | - //Printing shortest path from the given source src |
55 | | - for(int i=1;i<=n;i++){ |
56 | | - if(i!=src && w[src][i]!=1000000l){System.out.print(w[src][i]+" ");} |
57 | | - else if(i!=src){System.out.print("-1"+" ");} //Printing -1 if there is no path b/w given pair of edges |
58 | | - } |
59 | | - |
60 | | - } |
61 | 83 | } |
0 commit comments