Minimum Cost Walk in Weighted Graph - Problem

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

example_1.py โ€” Basic Connected Graph
$ Input: n = 5, edges = [[0,1,7],[1,3,7],[1,2,1]], query = [[0,3],[3,4]]
โ€บ Output: [1, -1]
๐Ÿ’ก Note: For query (0,3): vertices are connected, we can go 0โ†’1โ†’2โ†’1โ†’3. Since we can revisit edges, the minimum cost is the AND of all edges in the component: 7&7&1 = 1. For query (3,4): vertex 4 is not connected to the component containing 3, so return -1.
example_2.py โ€” Multiple Components
$ Input: n = 3, edges = [[0,2,7],[0,1,15],[1,2,6]], query = [[1,2]]
โ€บ Output: [6]
๐Ÿ’ก Note: All vertices are in the same component. The minimum cost path uses all edges: 15&7&6 = 6. Even though direct path 1โ†’2 has cost 6, the global minimum considering all possible paths (including revisiting edges) is still 6.
example_3.py โ€” Self Query Edge Case
$ Input: n = 2, edges = [], query = [[0,0],[0,1]]
โ€บ Output: [0, -1]
๐Ÿ’ก Note: Query (0,0): asking for path from vertex to itself always returns 0. Query (0,1): no edges exist, so vertices are disconnected, return -1.

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

Visualization

Tap to expand
Security Zone A0127 (111)5 (101)3 (011)Access Level: 7&5&3 = 1Security Zone B3Isolated - No AccessQuery ResultsPath 0โ†’2: Same zone โ†’ Cost = 1Path 0โ†’3: Different zones โ†’ Impossible (-1)
Understanding the Visualization
1
Identify Components
Use Union-Find to group connected vertices
2
Calculate Security Level
For each component, AND all edge weights to find minimum required permissions
3
Process Queries
Return component security level or -1 for disconnected vertices
Key Takeaway
๐ŸŽฏ Key Insight: In connected components with revisitable edges, the minimum cost is simply the AND of all edge weights in that component!
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
28.4K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen