data-structures · intermediate · ~15 min
Dijkstra on a dense matrix.
Weighted directed graph as a matrix: w[i*n+j] is a positive edge weight, or 0 for no edge.
int dijkstra_distance(const int *w,int n,int src,int dst);
Return the shortest-path distance src→dst, or −1 if unreachable. src==dst → 0.
w n×n, weights ≥1 or 0=no edge.
Shortest distance, or −1.
All weights positive (Dijkstra assumption).
#include <stddef.h>
/* Weighted directed graph: w[i*n+j] is a positive edge weight, or 0 for no edge. Shortest-path distance src->dst (Dijkstra), or -1 if unreachable. src==dst -> 0. */
int dijkstra_distance(const int *w,int n,int src,int dst){ (void)w;(void)n;(void)src;(void)dst; return -1; }
Treating weight 0 as a real edge; using BFS (ignores weights).
src==dst → 0; unreachable → −1.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.