Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
# [3243.Shortest Distance After Road Addition Queries I][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
You are given an integer `n` and a 2D integer array `queries`.

There are `n` cities numbered from `0` to `n - 1`. Initially, there is a **unidirectional** road from city `i` to city `i + 1` for all `0 <= i < n - 1`.

`queries[i] = [ui, vi]` represents the addition of a new **unidirectional** road from city `ui` to city `vi`. After each query, you need to find the **length** of the **shortest path** from city `0` to city `n - 1`.

**Example 1:**
Return an array `answer` where for each `i` in the range `[0, queries.length - 1]`, `answer[i]` is the length of the shortest path from city `0` to city `n - 1` after processing the **first** `i + 1` queries.

**Example 1:**

![1](./1.jpg)
![2](./2.jpg)
![3](./3.jpg)

```
Input: a = "11", b = "1"
Output: "100"
Input: n = 5, queries = [[2,4],[0,2],[0,4]]
Output: [3,2,1]
```

## 题意
> ...
**Example 2:**

## 题解
![4](./4.jpg)
![5](./5.jpg)

### 思路1
> ...
Shortest Distance After Road Addition Queries I
```go
```
Input: n = 4, queries = [[0,3],[0,2]]

Output: [1,1]
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(n int, queries [][]int) []int {
ans := make([]int, len(queries))
adj := make(map[int]map[int]struct{})
for i := range n {
adj[i] = make(map[int]struct{})
}
// 初始化路径
for i := range n - 1 {
adj[i][i+1] = struct{}{}
}
var bfs func() int
bfs = func() int {
queue := []int{0}
step := 0
used := map[int]struct{}{
0: {},
}
for len(queue) > 0 {
nq := make([]int, 0)
for _, q := range queue {
if q == n-1 {
return step
}
for rel := range adj[q] {
if _, ok := used[rel]; !ok {
nq = append(nq, rel)
used[rel] = struct{}{}
}
}
}
queue = nq
step++
}
return -1
}
for i, q := range queries {
adj[q[0]][q[1]] = struct{}{}
ans[i] = bfs()
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
n int
queries [][]int
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 5, [][]int{{2, 4}, {0, 2}, {0, 4}}, []int{3, 2, 1}},
{"TestCase2", 4, [][]int{{0, 3}, {0, 2}}, []int{1, 1}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.n, c.queries)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.n, c.queries)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading