Shortest Path with even number of Edges from Source to Destination | Java program for the above approach ; Adjacency List : to represent graph ; Distance Array : to store shortest distance to every node ; Returns value which will represent even_x ; Returns value which will represent odd_x ; Converting edge ( a -> b ) to 2 different edges i . e . ( a -> b ) converts to ( 1 ) . even_a -> odd_b ( 2 ) . odd_a -> even_b since , graph is undirected , so we push them in reverse order too hence , 4 push_back operations are there . ; Function calculates shortest distance to all nodes from " source " using Dijkstra Shortest Path Algorithm and returns shortest distance to " destination " ; Priority Queue / min - heap to store and process ( distance , node ) ; Pushing source node to priority queue and dist from source to source is set to 0 ; U is the node at top of the priority queue note that pq . top ( ) . first refers to the Distance and pq . top ( ) . second will refer to the Node ; Exploring all neighbours of node u ; v is neighbour node of u and c is the cost / weight of edge ( u , v ) ; Relaxation : checking if there is a shorter path to v via u ; Updating distance of v ; Returning shortest distance to " destination " ; Driver code ; n = number of Nodes , m = number of Edges ; If ans is INF : There is no even length path from source to destination else path exists and we print the shortest distance
import java . util . ArrayList ; import java . util . Arrays ; import java . util . PriorityQueue ; class GFG { static class Pair implements Comparable < Pair > { int first , second ; public Pair ( int first , int second ) { this . first = first ; this . second = second ; } @ Override public int compareTo ( GFG . Pair o ) { if ( this . first == o . first ) { return this . second - o . second ; } return this . first - o . first ; } } static final int MAXX = 10000 , INF = ( int ) 1e9 ; @ SuppressWarnings ( " unchecked " ) static ArrayList < Pair > [ ] adj = new ArrayList [ MAXX * 10 + 3 ] ; static int [ ] dist = new int [ MAXX * 10 + 3 ] ; static int even ( int x ) { return x * 10 + 2 ; } static int odd ( int x ) { return x * 10 + 1 ; } static void addEdge ( int a , int b , int cost ) { adj [ even ( a ) ] . add ( new Pair ( odd ( b ) , cost ) ) ; adj [ odd ( a ) ] . add ( new Pair ( even ( b ) , cost ) ) ; adj [ odd ( b ) ] . add ( new Pair ( even ( a ) , cost ) ) ; adj [ even ( b ) ] . add ( new Pair ( odd ( a ) , cost ) ) ; } static int dijkstra ( int source , int destination ) { PriorityQueue < Pair > pq = new PriorityQueue < > ( ) ; pq . add ( new Pair ( 0 , even ( source ) ) ) ; dist [ even ( source ) ] = 0 ; while ( ! pq . isEmpty ( ) ) { int u = pq . poll ( ) . second ; for ( Pair p : adj [ u ] ) { int v = p . first ; int c = p . second ; if ( dist [ u ] + c < dist [ v ] ) { dist [ v ] = dist [ u ] + c ; pq . add ( new Pair ( dist [ v ] , v ) ) ; } } } return dist [ even ( destination ) ] ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < MAXX * 10 + 3 ; i ++ ) { adj [ i ] = new ArrayList < Pair > ( ) ; } Arrays . fill ( dist , INF ) ; int n = 5 , m = 6 ; addEdge ( 1 , 2 , 1 ) ; addEdge ( 2 , 3 , 2 ) ; addEdge ( 2 , 5 , 15 ) ; addEdge ( 3 , 5 , 1 ) ; addEdge ( 3 , 4 , 4 ) ; addEdge ( 5 , 4 , 3 ) ; int source = 1 ; int destination = n ; int ans = dijkstra ( source , destination ) ; if ( ans == INF ) System . out . println ( " - 1" ) ; else System . out . println ( ans ) ; } }