3108. Minimum Cost Walk in Weighted Graph
There is an undirected weighted graph with
n
vertices labeled from0
ton - 1
.You are given the integer
n
and an arrayedges
, whereedges[i] = [ui, vi, wi]
indicates that there is an edge between verticesui
andvi
with a weight ofwi
.A walk on a graph is a sequence of vertices and edges. The walk starts and ends with a vertex, and each edge connects the vertex that comes before it and the vertex that comes after it. It’s important to note that a walk may visit the same edge or vertex more than once.
The cost of a walk starting at node
u
and ending at nodev
is defined as the bitwiseAND
of the weights of the edges traversed during the walk. In other words, if the sequence of edge weights encountered during the walk isw0, w1, w2, ..., wk
, then the cost is calculated asw0 & w1 & w2 & ... & wk
, where&
denotes the bitwiseAND
operator.You are also given a 2D array
query
, wherequery[i] = [si, ti]
. For each query, you need to find the minimum cost of the walk starting at vertexsi
and ending at vertexti
. If there exists no such walk, the answer is-1
.Return the array
answer
, whereanswer[i]
denotes the minimum cost of a walk for queryi
.
1 | class Solution { |