data-structures · intermediate · ~15 min
Prim's (or Kruskal's) MST.
Undirected weighted graph (symmetric w; 0 = no edge, positive = weight).
int mst_weight(const int *w,int n);
Return the total weight of a Minimum Spanning Tree, or −1 if the graph is disconnected.
w n×n symmetric, weights ≥1 or 0.
MST total weight, or −1.
n>=1; single vertex → 0.
#include <stddef.h>
/* Total weight of a Minimum Spanning Tree of an UNDIRECTED weighted graph (symmetric w; 0 = no edge, positive = weight). Return -1 if the graph is disconnected. */
int mst_weight(const int *w,int n){ (void)w;(void)n; return -1; }
Not detecting disconnection; adding an edge that reconnects an already-in-tree vertex.
Disconnected → −1; single vertex → 0.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.