Shortest Path in Graph with Weights as either 1 or 2
Last Updated : 13 Jun, 2026
Given a weighted undirected graph with V vertices numbered from 0 to V - 1, represented by an array edges, where edges[i] = [ui, vi, wi] indicates that there is an edge between vertices ui and vi with a weight of wi. (wi can only be 1 or 2), and two vertices src and dest, find the shortest distance from src to dest.
The shortest distance is defined as the minimum total weight required to reach dest starting from src.
Return the shortest distance from src to dest. If dest is not reachable from src, return -1.
Output: 3 Explanation: One of the shortest paths from vertex 0 to vertex 3 is 0 -> 1 -> 3 with a total weight of 1 + 2 = 3. Another shortest path is 0 -> 2 -> 3 with a total weight of 2 + 1 = 3. Hence, the shortest distance from 0 to 3 is 3.
Output: 2 Explanation: The shortest path from vertex 1 to vertex 4 is 1 -> 3 -> 4 with a total weight of 1 + 1 = 2. Hence, the shortest distance from 1 to 4 is 2.
[Better Approach] - Using Dijkstra algorith - O(E + V log V) and O(V + E) Space
The idea is to use Dijkstra’s shortest path algorithm where we greedily select the node with the minimum current distance, finalize its shortest distance, and use it to relax all adjacent edges. By repeatedly choosing the closest unvisited node, we can find the shortest path from the source to all other nodes in a graph with non-negative edge weights.
[Expected Approach] - Using Edge Splitting Technique + BFS - O(V + E) Time and O(V + E) Space
The idea is to convert the weighted graph into an unweighted graph by splitting every edge with weight 2 into two edges of weight 1 using an intermediate node. After this transformation, the shortest path can be found using simple BFS in O(V + E) time.
C++
#include<iostream>#include<vector>#include<queue>usingnamespacestd;intshortestPath(intV,intsrc,intdest,vector<vector<int>>&edges){intextra=V;// Create adjacency list. Extra nodes// are used to split weight 2 edges.vector<vector<int>>adj(V+edges.size());for(auto&e:edges){intu=e[0];intv=e[1];intwt=e[2];if(wt==1){// Weight 1 edge remains unchanged.adj[u].push_back(v);adj[v].push_back(u);}else{// Convert weight 2 edge into two weight 1 edges:// u -- 1 -- newNode -- 1 -- vadj[u].push_back(extra);adj[extra].push_back(v);adj[v].push_back(extra);adj[extra].push_back(u);extra++;}}// BFS on the transformed unweighted// graph gives shortest distance.vector<int>dist(extra,-1);queue<int>q;q.push(src);dist[src]=0;while(!q.empty()){intnode=q.front();q.pop();if(node==dest)returndist[node];for(intnxt:adj[node]){if(dist[nxt]==-1){dist[nxt]=dist[node]+1;q.push(nxt);}}}// Destination is not reachable from source.return-1;}intmain(){intV=4;vector<vector<int>>edges={{0,1,1},{0,2,2},{2,3,1},{1,2,1},{1,3,2}};intsrc=0,dest=3;cout<<shortestPath(V,src,dest,edges)<<endl;return0;}
Java
importjava.util.ArrayList;importjava.util.List;importjava.util.Queue;importjava.util.Arrays;importjava.util.LinkedList;publicclassGFG{staticintshortestPath(intV,intsrc,intdest,int[][]edges){intextra=V;// Create adjacency list. Extra nodes// are used to split weight 2 edges.List<List<Integer>>adj=newArrayList<>();for(inti=0;i<V+edges.length;i++)adj.add(newArrayList<>());for(int[]e:edges){intu=e[0],v=e[1],wt=e[2];if(wt==1){// Weight 1 edge remains unchanged.adj.get(u).add(v);adj.get(v).add(u);}else{// Convert weight 2 edge into two weight 1 edges:// u -- 1 -- newNode -- 1 -- vadj.get(u).add(extra);adj.get(extra).add(v);adj.get(v).add(extra);adj.get(extra).add(u);extra++;}}// BFS on the transformed unweighted// graph gives shortest distance.int[]dist=newint[extra];Arrays.fill(dist,-1);Queue<Integer>q=newLinkedList<>();q.add(src);dist[src]=0;while(!q.isEmpty()){intnode=q.poll();if(node==dest)returndist[node];for(intnxt:adj.get(node)){if(dist[nxt]==-1){dist[nxt]=dist[node]+1;q.add(nxt);}}}// Destination is not reachable from source.return-1;}publicstaticvoidmain(String[]args){intV=4;int[][]edges={{0,1,1},{0,2,2},{2,3,1},{1,2,1},{1,3,2}};intsrc=0,dest=3;System.out.println(shortestPath(V,src,dest,edges));}}
Python
fromcollectionsimportdequedefshortestPath(V,src,dest,edges):extra=V# Create adjacency list. Extra nodes# are used to split weight 2 edges.adj=[[]for_inrange(V+len(edges))]foru,v,wtinedges:ifwt==1:# Weight 1 edge remains unchanged.adj[u].append(v)adj[v].append(u)else:# Convert weight 2 edge into two weight 1 edges:# u -- 1 -- newNode -- 1 -- vadj[u].append(extra)adj[extra].append(v)adj[v].append(extra)adj[extra].append(u)extra+=1# BFS on the transformed unweighted# graph gives shortest distance.dist=[-1]*extradist[src]=0q=deque([src])whileq:node=q.popleft()ifnode==dest:returndist[node]fornxtinadj[node]:ifdist[nxt]==-1:dist[nxt]=dist[node]+1q.append(nxt)# Destination is not reachable from source.return-1if__name__=="__main__":V=4edges=[[0,1,1],[0,2,2],[2,3,1],[1,2,1],[1,3,2]]src,dest=0,3print(shortestPath(V,src,dest,edges))
C#
usingSystem;usingSystem.Collections.Generic;classGFG{staticintShortestPath(intV,intsrc,intdest,int[][]edges){intextra=V;// Create adjacency list. Extra nodes// are used to split weight 2 edges.varadj=newList<List<int>>();for(inti=0;i<V+edges.Length;i++)adj.Add(newList<int>());foreach(vareinedges){intu=e[0],v=e[1],wt=e[2];if(wt==1){// Weight 1 edge remains unchanged.adj[u].Add(v);adj[v].Add(u);}else{// Convert weight 2 edge into two weight 1 edges:// u -- 1 -- newNode -- 1 -- vadj[u].Add(extra);adj[extra].Add(v);adj[v].Add(extra);adj[extra].Add(u);extra++;}}// BFS on the transformed unweighted// graph gives shortest distance.int[]dist=newint[extra];Array.Fill(dist,-1);varq=newQueue<int>();q.Enqueue(src);dist[src]=0;while(q.Count>0){intnode=q.Dequeue();if(node==dest)returndist[node];foreach(intnxtinadj[node]){if(dist[nxt]==-1){dist[nxt]=dist[node]+1;q.Enqueue(nxt);}}}// Destination is not reachable from source.return-1;}staticvoidMain(){intV=4;int[][]edges={new[]{0,1,1},new[]{0,2,2},new[]{2,3,1},new[]{1,2,1},new[]{1,3,2}};intsrc=0,dest=3;Console.WriteLine(ShortestPath(V,src,dest,edges));}}
JavaScript
functionshortestPath(V,src,dest,edges){letextra=V;// Create adjacency list. Extra nodes// are used to split weight 2 edges.constadj=Array.from({length:V+edges.length},()=>[]);for(const[u,v,wt]ofedges){if(wt===1){// Weight 1 edge remains unchanged.adj[u].push(v);adj[v].push(u);}else{// Convert weight 2 edge into two weight 1 edges:// u -- 1 -- newNode -- 1 -- vadj[u].push(extra);adj[extra].push(v);adj[v].push(extra);adj[extra].push(u);extra++;}}// BFS on the transformed unweighted// graph gives shortest distance.constdist=newArray(extra).fill(-1);constq=[src];dist[src]=0;while(q.length>0){constnode=q.shift();if(node===dest)returndist[node];for(constnxtofadj[node]){if(dist[nxt]===-1){dist[nxt]=dist[node]+1;q.push(nxt);}}}// Destination is not reachable from source.return-1;}// Driver codeconstV=4;constedges=[[0,1,1],[0,2,2],[2,3,1],[1,2,1],[1,3,2]];constsrc=0,dest=3;console.log(shortestPath(V,src,dest,edges));