Build a Weighted Adjacency List
core
Represent a directed, weighted graph on nodes 0 through n-1 as a weighted adjacency list. Each edge arrives as (u, v, w): a directed edge from u to v with weight w. Implement build_wadj(n, edges) so that for every edge you push the pair (v, w) (destination first, then weight) onto adj[u]; adj[v] is not touched by this edge. A self-loop (u, u, w) pushes (u, w) onto adj[u] exactly once (the graph is directed, so this is not doubled the way an undirected self-loop would be). Parallel edges between the same u and v with different weights must all be kept, not merged.
Example: build_wadj(3, {(0,1,5), (1,2,3), (0,2,10)}) returns adj[0] = {(1,5), (2,10)}, adj[1] = {(2,3)}, adj[2] = {}: each entry is (destination, weight), and node 2 has no outgoing edges.
Input format: each test in tests/*.in is laid out as:
- Line 1:
n e, the node count and the number of directed edges; nodes are numbered0ton - 1. - Next
elines:u v w, one directed edge fromutovwith weightw. Wheneis0the file is just the header line.
Test 04.in reads:
2 0
This is 2 nodes with no edges: the header is the whole test, and both adjacency rows come back empty.
Where you'll use it:
✦ Solution & editorial unlock with the pass.