Skip to content

Commit

Permalink
Merge pull request #629 from LetMeFly666/3243
Browse files Browse the repository at this point in the history
添加问题“3243.新增道路查询后的最短距离I”的代码和题解
  • Loading branch information
LetMeFly666 authored Nov 19, 2024
2 parents 200fde9 + 575c899 commit 92ec86c
Show file tree
Hide file tree
Showing 11 changed files with 1,742 additions and 8 deletions.
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"files.associations": {
"*.pro": "plaintext",
"*.wxss": "css",
"*.wxml": "html",
"array": "cpp",
"atomic": "cpp",
"*.tcc": "cpp",
Expand Down Expand Up @@ -80,7 +83,8 @@
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"set": "cpp"
"set": "cpp",
"math.h": "c"
},
"editor.mouseWheelZoom": true,
"workbench.tree.enableStickyScroll": true,
Expand Down
33 changes: 33 additions & 0 deletions Codes/3243-shortest-distance-after-road-addition-queries-i.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* @Author: LetMeFly
* @Date: 2024-11-19 14:30:16
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2024-11-19 14:37:15
*/
#ifdef _WIN32
#include "_[1,2]toVector.h"
#endif

class Solution {
public:
vector<int> shortestDistanceAfterQueries(int n, vector<vector<int>>& queries) {
vector<int> shortest(n);
vector<vector<int>> fromList(n);
for (int i = 1; i < n; i++) {
fromList[i].push_back(i - 1);
shortest[i] = i;
}
vector<int> ans(queries.size());
for (int i = 0; i < queries.size(); i++) {
int from = queries[i][0], to = queries[i][1];
fromList[to].push_back(from);
for (int j = to; j < n; j++) {
for (int from : fromList[j]) {
shortest[j] = min(shortest[j], shortest[from] + 1);
}
}
ans[i] = shortest.back();
}
return ans;
}
};
28 changes: 28 additions & 0 deletions Codes/3243-shortest-distance-after-road-addition-queries-i.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* @Author: LetMeFly
* @Date: 2024-11-19 15:00:03
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2024-11-19 15:04:56
*/
package main

func shortestDistanceAfterQueries(n int, queries [][]int) (ans []int) {
shortest := make([]int, n)
fromList := make([][]int, n)
for i := range shortest {
shortest[i] = i
fromList[i] = make([]int, 0)
fromList[i] = append(fromList[i], i - 1)
}
ans = make([]int, len(queries))
for i, query := range queries {
fromList[query[1]] = append(fromList[query[1]], query[0])
for j := query[1]; j < n; j++ {
for _, from := range fromList[j] {
shortest[j] = min(shortest[j], shortest[from] + 1)
}
}
ans[i] = shortest[n - 1]
}
return
}
32 changes: 32 additions & 0 deletions Codes/3243-shortest-distance-after-road-addition-queries-i.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* @Author: LetMeFly
* @Date: 2024-11-19 14:46:11
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2024-11-19 14:58:40
*/
import java.util.ArrayList;
import java.util.List;

class Solution {
public int[] shortestDistanceAfterQueries(int n, int[][] queries) {
int[] shortest = new int[n];
List<Integer>[] fromList = new List[n];
for (int i = 0; i < n; i++) {
shortest[i] = i;
fromList[i] = new ArrayList<Integer>();
fromList[i].add(i - 1);
}
int[] ans = new int[queries.length];
for (int i = 0; i < queries.length; i++) {
int from = queries[i][0], to = queries[i][1];
fromList[to].add(from);
for (int j = to; j < n; j++) {
for (int from_ : fromList[j]) {
shortest[j] = Math.min(shortest[j], shortest[from_] + 1);
}
}
ans[i] = shortest[n - 1];
}
return ans;
}
}
19 changes: 19 additions & 0 deletions Codes/3243-shortest-distance-after-road-addition-queries-i.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'''
Author: LetMeFly
Date: 2024-11-19 14:37:55
LastEditors: LetMeFly.xyz
LastEditTime: 2024-11-19 14:45:39
'''
from typing import List

class Solution:
def shortestDistanceAfterQueries(self, n: int, queries: List[List[int]]) -> List[int]:
shortest = [i for i in range(n)]
fromList = [[i - 1] for i in range(n)]
ans = []
for from_, to in queries:
fromList[to].append(from_)
for i in range(to, n):
shortest[i] = min(shortest[i], min(shortest[j] + 1 for j in fromList[i]))
ans.append(shortest[-1])
return ans
Loading

0 comments on commit 92ec86c

Please sign in to comment.