Imagine you're exploring a network of cities connected by toll roads, where each road has a unique toll fee. Your goal is to find the cheapest possible journey between any two cities, but with a twist!
In this problem, you have an undirected weighted graph with n vertices (labeled 0 to n-1) and edges with weights. The cost of traveling isn't calculated by adding up toll fees - instead, it's calculated using the bitwise AND operation of all edge weights in your path.
Here's the key insight: you can revisit the same roads and cities multiple times in your journey! This means if taking a detour through previously visited places gives you a cheaper total cost (lower bitwise AND result), you should take it.
Input: An integer n (number of vertices), an array edges where edges[i] = [u_i, v_i, w_i] represents an edge between vertices u_i and v_i with weight w_i, and a 2D array query where query[i] = [s_i, t_i] represents queries for minimum cost walks.
Output: An array where each element is the minimum cost walk for the corresponding query, or -1 if no path exists.
Input & Output
Constraints
- 1 โค n โค 105
- 0 โค edges.length โค 2 ร 105
- edges[i].length == 3
- 0 โค ui, vi โค n - 1
- ui โ vi
- 1 โค wi โค 106
- 1 โค query.length โค 2 ร 105
- query[i].length == 2
- 0 โค si, ti โค n - 1