Monotone shortest paths

Given a finite directed weighted graph, a source vertex s, and the graph's edge weights, compute the shortest monotone-path distance from s to every vertex. A path is monotone if its edge weights are strictly increasing or strictly decreasing. For each destination, return the smaller distance among the two kinds of monotone paths. The empty path from s to itself has distance 0.

The input is a text encoding: the first line contains n m s, followed by m lines u v w. Vertices are 0..n-1, weights are finite nonnegative integers, and parallel directed edges are allowed. Return an array of length n; use -1 when no increasing or decreasing monotone path reaches a vertex.

This is the single-source, all-destinations exercise corresponding to Algorithms 4.4.34. Strictness applies to consecutive edge weights. It does not impose a simple-vertex restriction: the strict edge-weight order makes the edge-state dependency acyclic even if the original graph contains a directed cycle.

Examples
Input: "1 0 0\n"
Output: [0]
Hints

Monotone shortest paths

Given a finite directed weighted graph, a source vertex `s`, and the graph's edge weights, compute the shortest monotone-path distance from `s` to every vertex. A path is monotone if its edge weights are **strictly increasing** or **strictly decreasing**. For each destination, return the smaller distance among the two kinds of monotone paths. The empty path from `s` to itself has distance `0`.