Skip to content

Commit a366b26

Browse files
authored
Merge pull request #1155 from 0xff-dev/3108
Add solution and test-cases for problem 3108
2 parents 514fb16 + 4d1d124 commit a366b26

File tree

5 files changed

+91
-25
lines changed

5 files changed

+91
-25
lines changed
7.68 KB
Loading
8.28 KB
Loading

leetcode/3101-3200/3108.Minimum-Cost-Walk-in-Weighted-Graph/README.md

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,48 @@
11
# [3108.Minimum Cost Walk in Weighted Graph][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
There is an undirected weighted graph with `n` vertices labeled from `0` to `n - 1`.
5+
6+
You are given the integer `n` and an array `edges`, where `edges[i] = [ui, vi, wi]` indicates that there is an edge between vertices `ui` and `vi` with a weight of `wi`.
7+
8+
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.
9+
10+
The **cost** of a walk starting at node `u` and ending at node `v` is defined as the bitwise `AND` of the weights of the edges traversed during the walk. In other words, if the sequence of edge weights encountered during the walk is `w0, w1, w2, ..., wk`, then the cost is calculated as `w0 & w1 & w2 & ... & wk`, where `&` denotes the bitwise `AND` operator.
11+
12+
You are also given a 2D array `query`, where `query[i] = [si, ti]`. For each query, you need to find the minimum cost of the walk starting at vertex `si` and ending at vertex `ti`. If there exists no such walk, the answer is `-1`.
13+
14+
Return the array `answer`, where `answer[i]` denotes the **minimum** cost of a walk for query `i`.
15+
16+
**Example 1:**
17+
18+
![1](./1.png)
719

8-
**Example 1:**
920

1021
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
22+
Input: n = 5, edges = [[0,1,7],[1,3,7],[1,2,1]], query = [[0,3],[3,4]]
23+
24+
Output: [1,-1]
25+
26+
Explanation:
27+
28+
To achieve the cost of 1 in the first query, we need to move on the following edges: 0->1 (weight 7), 1->2 (weight 1), 2->1 (weight 1), 1->3 (weight 7).
29+
30+
In the second query, there is no walk between nodes 3 and 4, so the answer is -1.
1331
```
1432

15-
## 题意
16-
> ...
33+
**Example 2:**
34+
35+
![2](./2.png)
1736

18-
## 题解
1937

20-
### 思路1
21-
> ...
22-
Minimum Cost Walk in Weighted Graph
23-
```go
2438
```
39+
Input: n = 3, edges = [[0,2,7],[0,1,15],[1,2,6],[1,2,1]], query = [[1,2]]
40+
41+
Output: [0]
2542
43+
Explanation:
44+
To achieve the cost of 0 in the first query, we need to move on the following edges: 1->2 (weight 1), 2->1 (weight 6), 1->2 (weight 1).
45+
```
2646

2747
## 结语
2848

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,50 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
type unionFind3108 struct {
4+
father []int
5+
weight []int
6+
}
7+
8+
func (u *unionFind3108) find(x int) int {
9+
if u.father[x] != x {
10+
u.father[x] = u.find(u.father[x])
11+
}
12+
return u.father[x]
13+
}
14+
15+
func (u *unionFind3108) union(x, y, w int) {
16+
fx := u.find(x)
17+
fy := u.find(y)
18+
if fx < fy {
19+
u.father[fy] = fx
20+
u.weight[fx] = u.weight[fx] & u.weight[fy] & w
21+
} else {
22+
u.father[fx] = fy
23+
u.weight[fy] = u.weight[fx] & u.weight[fy] & w
24+
}
25+
}
26+
27+
func Solution(n int, edges [][]int, query [][]int) []int {
28+
u := &unionFind3108{father: make([]int, n), weight: make([]int, n)}
29+
for i := range n {
30+
u.father[i] = i
31+
u.weight[i] = 0xffffffff
32+
}
33+
34+
for _, e := range edges {
35+
f, t, w := e[0], e[1], e[2]
36+
u.union(f, t, w)
37+
}
38+
ans := make([]int, len(query))
39+
for i, q := range query {
40+
f, t := q[0], q[1]
41+
ff := u.find(f)
42+
tf := u.find(t)
43+
if ff != tf {
44+
ans[i] = -1
45+
continue
46+
}
47+
ans[i] = u.weight[ff]
48+
}
49+
return ans
550
}

leetcode/3101-3200/3108.Minimum-Cost-Walk-in-Weighted-Graph/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
n int
14+
edges [][]int
15+
query [][]int
16+
expect []int
1517
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
18+
{"TestCase1", 5, [][]int{{0, 1, 7}, {1, 3, 7}, {1, 2, 1}}, [][]int{{0, 3}, {3, 4}}, []int{1, -1}},
19+
{"TestCase2", 3, [][]int{{0, 2, 7}, {0, 1, 15}, {1, 2, 6}, {1, 2, 1}}, [][]int{{1, 2}}, []int{0}},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.n, c.edges, c.query)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
28+
c.expect, got, c.n, c.edges, c.query)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)