diff --git a/Dijkshtra.java b/Dijkshtra.java index 93c58d670..ed05ef343 100644 --- a/Dijkshtra.java +++ b/Dijkshtra.java @@ -1,20 +1,28 @@ public static void main(String[] args) throws IOException { Reader in=new Reader(); int t1=in.nextInt(); - for(int gj=0;gjcmp){ - w[x][y]=cmp; w[y][x]=cmp; + if(w[x][y]>cmp){ //Comparing previous edge value with current value - Cycle Case + w[x][y]=cmp; w[y][x]=cmp; } } + + //Implementing Dijkshtra's Algorithm + Stack t=new Stack(); int src=in.nextInt(); for(int i=1;i<=n;i++){ @@ -30,9 +38,11 @@ public static void main(String[] args) throws IOException { min=(int) w[src][t.elementAt(i)];loc=i;} } p.push(t.elementAt(loc));t.removeElementAt(loc);} + + //Printing shortest path from the given source src for(int i=1;i<=n;i++){ if(i!=src && w[src][i]!=1000000l){System.out.print(w[src][i]+" ");} - else if(i!=src){System.out.print("-1"+" ");} - }System.out.println(); + else if(i!=src){System.out.print("-1"+" ");} //Printing -1 if there is no path b/w given pair of edges } + }