A wireless mesh network has n nodes, numbered 0 to n - 1, and m two-way links.
Link i connects nodes u and v and can carry w units of bandwidth. Data sent along a multi-hop
route can only flow as fast as the slowest (minimum-bandwidth) link on that route.
Given a source node s and a destination node t (with s != t), print the highest achievable
end-to-end bandwidth over any route from s to t, where a route's bandwidth is the minimum link
weight along it. If t cannot be reached from s, print -1 instead.
Line 1: two integers n m.
Next m lines: three integers u v w -- an undirected link between nodes u and v with bandwidth
w (nodes are 0-indexed).
Last line: two integers s t.
A single integer: the maximum possible end-to-end bandwidth from s to t, or -1 if unreachable.
Example 1
Input
4 4 0 1 10 1 3 2 0 2 5 2 3 4 0 3
Expected
4
Explanation
Route 0 -> 2 -> 3 has a weakest link of 4 (min(5,4)), which beats route 0 -> 1 -> 3 whose weakest link is only 2 (min(10,2)), so the strongest achievable end-to-end capacity is 4.
Example 2
Input
3 1 0 1 7 0 2
Expected
-1
Explanation
Node 2 has no link to node 0 at all, so no route exists and the answer is -1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →