Skip to content

Commit 92ec86c

Browse files
authored
Merge pull request #629 from LetMeFly666/3243
添加问题“3243.新增道路查询后的最短距离I”的代码和题解
2 parents 200fde9 + 575c899 commit 92ec86c

11 files changed

+1742
-8
lines changed

.vscode/settings.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"files.associations": {
3+
"*.pro": "plaintext",
4+
"*.wxss": "css",
5+
"*.wxml": "html",
36
"array": "cpp",
47
"atomic": "cpp",
58
"*.tcc": "cpp",
@@ -80,7 +83,8 @@
8083
"iterator": "cpp",
8184
"memory": "cpp",
8285
"memory_resource": "cpp",
83-
"set": "cpp"
86+
"set": "cpp",
87+
"math.h": "c"
8488
},
8589
"editor.mouseWheelZoom": true,
8690
"workbench.tree.enableStickyScroll": true,
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-11-19 14:30:16
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-11-19 14:37:15
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
vector<int> shortestDistanceAfterQueries(int n, vector<vector<int>>& queries) {
14+
vector<int> shortest(n);
15+
vector<vector<int>> fromList(n);
16+
for (int i = 1; i < n; i++) {
17+
fromList[i].push_back(i - 1);
18+
shortest[i] = i;
19+
}
20+
vector<int> ans(queries.size());
21+
for (int i = 0; i < queries.size(); i++) {
22+
int from = queries[i][0], to = queries[i][1];
23+
fromList[to].push_back(from);
24+
for (int j = to; j < n; j++) {
25+
for (int from : fromList[j]) {
26+
shortest[j] = min(shortest[j], shortest[from] + 1);
27+
}
28+
}
29+
ans[i] = shortest.back();
30+
}
31+
return ans;
32+
}
33+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-11-19 15:00:03
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-11-19 15:04:56
6+
*/
7+
package main
8+
9+
func shortestDistanceAfterQueries(n int, queries [][]int) (ans []int) {
10+
shortest := make([]int, n)
11+
fromList := make([][]int, n)
12+
for i := range shortest {
13+
shortest[i] = i
14+
fromList[i] = make([]int, 0)
15+
fromList[i] = append(fromList[i], i - 1)
16+
}
17+
ans = make([]int, len(queries))
18+
for i, query := range queries {
19+
fromList[query[1]] = append(fromList[query[1]], query[0])
20+
for j := query[1]; j < n; j++ {
21+
for _, from := range fromList[j] {
22+
shortest[j] = min(shortest[j], shortest[from] + 1)
23+
}
24+
}
25+
ans[i] = shortest[n - 1]
26+
}
27+
return
28+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-11-19 14:46:11
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-11-19 14:58:40
6+
*/
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
class Solution {
11+
public int[] shortestDistanceAfterQueries(int n, int[][] queries) {
12+
int[] shortest = new int[n];
13+
List<Integer>[] fromList = new List[n];
14+
for (int i = 0; i < n; i++) {
15+
shortest[i] = i;
16+
fromList[i] = new ArrayList<Integer>();
17+
fromList[i].add(i - 1);
18+
}
19+
int[] ans = new int[queries.length];
20+
for (int i = 0; i < queries.length; i++) {
21+
int from = queries[i][0], to = queries[i][1];
22+
fromList[to].add(from);
23+
for (int j = to; j < n; j++) {
24+
for (int from_ : fromList[j]) {
25+
shortest[j] = Math.min(shortest[j], shortest[from_] + 1);
26+
}
27+
}
28+
ans[i] = shortest[n - 1];
29+
}
30+
return ans;
31+
}
32+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-11-19 14:37:55
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2024-11-19 14:45:39
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def shortestDistanceAfterQueries(self, n: int, queries: List[List[int]]) -> List[int]:
11+
shortest = [i for i in range(n)]
12+
fromList = [[i - 1] for i in range(n)]
13+
ans = []
14+
for from_, to in queries:
15+
fromList[to].append(from_)
16+
for i in range(to, n):
17+
shortest[i] = min(shortest[i], min(shortest[j] + 1 for j in fromList[i]))
18+
ans.append(shortest[-1])
19+
return ans

0 commit comments

Comments
 (0)