|
53 | 53 |
|
54 | 54 | <!-- 这里可写通用的实现逻辑 -->
|
55 | 55 |
|
| 56 | +**方法一:堆优化 Dijkstra + 记忆化搜索** |
| 57 | + |
56 | 58 | <!-- tabs:start -->
|
57 | 59 |
|
58 | 60 | ### **Python3**
|
59 | 61 |
|
60 | 62 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
61 | 63 |
|
62 | 64 | ```python
|
| 65 | +class Solution: |
| 66 | + def countRestrictedPaths(self, n: int, edges: List[List[int]]) -> int: |
| 67 | + @cache |
| 68 | + def dfs(i): |
| 69 | + if i == n: |
| 70 | + return 1 |
| 71 | + ans = 0 |
| 72 | + for j, _ in g[i]: |
| 73 | + if dist[i] > dist[j]: |
| 74 | + ans = (ans + dfs(j)) % mod |
| 75 | + return ans |
63 | 76 |
|
| 77 | + g = defaultdict(list) |
| 78 | + for u, v, w in edges: |
| 79 | + g[u].append((v, w)) |
| 80 | + g[v].append((u, w)) |
| 81 | + q = [(0, n)] |
| 82 | + dist = [inf] * (n + 1) |
| 83 | + dist[n] = 0 |
| 84 | + mod = 10**9 + 7 |
| 85 | + while q: |
| 86 | + _, u = heappop(q) |
| 87 | + for v, w in g[u]: |
| 88 | + if dist[v] > dist[u] + w: |
| 89 | + dist[v] = dist[u] + w |
| 90 | + heappush(q, (dist[v], v)) |
| 91 | + return dfs(1) |
64 | 92 | ```
|
65 | 93 |
|
66 | 94 | ### **Java**
|
67 | 95 |
|
68 | 96 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
69 | 97 |
|
70 | 98 | ```java
|
| 99 | +class Solution { |
| 100 | + private static final int INF = Integer.MAX_VALUE; |
| 101 | + private static final int MOD = (int) 1e9 + 7; |
| 102 | + private List<int[]>[] g; |
| 103 | + private int[] dist; |
| 104 | + private int[] f; |
| 105 | + private int n; |
| 106 | + |
| 107 | + public int countRestrictedPaths(int n, int[][] edges) { |
| 108 | + this.n = n; |
| 109 | + g = new List[n + 1]; |
| 110 | + for (int i = 0; i < g.length; ++i) { |
| 111 | + g[i] = new ArrayList<>(); |
| 112 | + } |
| 113 | + for (int[] e : edges) { |
| 114 | + int u = e[0], v = e[1], w = e[2]; |
| 115 | + g[u].add(new int[]{v, w}); |
| 116 | + g[v].add(new int[]{u, w}); |
| 117 | + } |
| 118 | + PriorityQueue<int[]> q = new PriorityQueue<>((a, b) -> a[0] - b[0]); |
| 119 | + q.offer(new int[]{0, n}); |
| 120 | + dist = new int[n + 1]; |
| 121 | + f = new int[n + 1]; |
| 122 | + Arrays.fill(dist, INF); |
| 123 | + Arrays.fill(f, -1); |
| 124 | + dist[n] = 0; |
| 125 | + while (!q.isEmpty()) { |
| 126 | + int[] p = q.poll(); |
| 127 | + int u = p[1]; |
| 128 | + for (int[] ne : g[u]) { |
| 129 | + int v = ne[0], w = ne[1]; |
| 130 | + if (dist[v] > dist[u] + w) { |
| 131 | + dist[v] = dist[u] + w; |
| 132 | + q.offer(new int[]{dist[v], v}); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + return dfs(1); |
| 137 | + } |
| 138 | + |
| 139 | + private int dfs(int i) { |
| 140 | + if (f[i] != -1) { |
| 141 | + return f[i]; |
| 142 | + } |
| 143 | + if (i == n) { |
| 144 | + return 1; |
| 145 | + } |
| 146 | + int ans = 0; |
| 147 | + for (int[] ne : g[i]) { |
| 148 | + int j = ne[0]; |
| 149 | + if (dist[i] > dist[j]) { |
| 150 | + ans = (ans + dfs(j)) % MOD; |
| 151 | + } |
| 152 | + } |
| 153 | + f[i] = ans; |
| 154 | + return ans; |
| 155 | + } |
| 156 | +} |
| 157 | +``` |
| 158 | + |
| 159 | +### **C++** |
| 160 | + |
| 161 | +```cpp |
| 162 | +using pii = pair<int, int>; |
| 163 | + |
| 164 | +class Solution { |
| 165 | +public: |
| 166 | + const int inf = INT_MAX; |
| 167 | + const int mod = 1e9 + 7; |
| 168 | + vector<vector<pii>> g; |
| 169 | + vector<int> dist; |
| 170 | + vector<int> f; |
| 171 | + int n; |
| 172 | + |
| 173 | + int countRestrictedPaths(int n, vector<vector<int>>& edges) { |
| 174 | + this->n = n; |
| 175 | + g.resize(n + 1); |
| 176 | + dist.assign(n + 1, inf); |
| 177 | + f.assign(n + 1, -1); |
| 178 | + dist[n] = 0; |
| 179 | + for (auto& e : edges) |
| 180 | + { |
| 181 | + int u = e[0], v = e[1], w = e[2]; |
| 182 | + g[u].emplace_back(v, w); |
| 183 | + g[v].emplace_back(u, w); |
| 184 | + } |
| 185 | + priority_queue<pii, vector<pii>, greater<pii>> q; |
| 186 | + q.emplace(0, n); |
| 187 | + while (!q.empty()) |
| 188 | + { |
| 189 | + auto [_, u] = q.top(); |
| 190 | + q.pop(); |
| 191 | + for (auto [v, w] : g[u]) |
| 192 | + { |
| 193 | + if (dist[v] > dist[u] + w) |
| 194 | + { |
| 195 | + dist[v] = dist[u] + w; |
| 196 | + q.emplace(dist[v], v); |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + return dfs(1); |
| 201 | + } |
| 202 | + |
| 203 | + int dfs(int i) { |
| 204 | + if (f[i] != -1) return f[i]; |
| 205 | + if (i == n) return 1; |
| 206 | + int ans = 0; |
| 207 | + for (auto [j, _] : g[i]) |
| 208 | + { |
| 209 | + if (dist[i] > dist[j]) |
| 210 | + { |
| 211 | + ans = (ans + dfs(j)) % mod; |
| 212 | + } |
| 213 | + } |
| 214 | + f[i] = ans; |
| 215 | + return ans; |
| 216 | + } |
| 217 | +}; |
| 218 | +``` |
| 219 | + |
| 220 | +### **Go** |
| 221 | + |
| 222 | +```go |
| 223 | +const inf = math.MaxInt32 |
| 224 | +const mod = 1e9 + 7 |
| 225 | + |
| 226 | +type pair struct { |
| 227 | + first int |
| 228 | + second int |
| 229 | +} |
| 230 | + |
| 231 | +var _ heap.Interface = (*pairs)(nil) |
| 232 | + |
| 233 | +type pairs []pair |
| 234 | + |
| 235 | +func (a pairs) Len() int { return len(a) } |
| 236 | +func (a pairs) Less(i int, j int) bool { |
| 237 | + return a[i].first < a[j].first || a[i].first == a[j].first && a[i].second < a[j].second |
| 238 | +} |
| 239 | +func (a pairs) Swap(i int, j int) { a[i], a[j] = a[j], a[i] } |
| 240 | +func (a *pairs) Push(x interface{}) { *a = append(*a, x.(pair)) } |
| 241 | +func (a *pairs) Pop() interface{} { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } |
71 | 242 |
|
| 243 | +func countRestrictedPaths(n int, edges [][]int) int { |
| 244 | + g := make([]pairs, n+1) |
| 245 | + for _, e := range edges { |
| 246 | + u, v, w := e[0], e[1], e[2] |
| 247 | + g[u] = append(g[u], pair{v, w}) |
| 248 | + g[v] = append(g[v], pair{u, w}) |
| 249 | + } |
| 250 | + dist := make([]int, n+1) |
| 251 | + f := make([]int, n+1) |
| 252 | + for i := range dist { |
| 253 | + dist[i] = inf |
| 254 | + f[i] = -1 |
| 255 | + } |
| 256 | + dist[n] = 0 |
| 257 | + h := make(pairs, 0) |
| 258 | + heap.Push(&h, pair{0, n}) |
| 259 | + for len(h) > 0 { |
| 260 | + u := heap.Pop(&h).(pair).second |
| 261 | + for _, ne := range g[u] { |
| 262 | + v, w := ne.first, ne.second |
| 263 | + if dist[v] > dist[u]+w { |
| 264 | + dist[v] = dist[u] + w |
| 265 | + heap.Push(&h, pair{dist[v], v}) |
| 266 | + } |
| 267 | + } |
| 268 | + } |
| 269 | + var dfs func(int) int |
| 270 | + dfs = func(i int) int { |
| 271 | + if f[i] != -1 { |
| 272 | + return f[i] |
| 273 | + } |
| 274 | + if i == n { |
| 275 | + return 1 |
| 276 | + } |
| 277 | + ans := 0 |
| 278 | + for _, ne := range g[i] { |
| 279 | + j := ne.first |
| 280 | + if dist[i] > dist[j] { |
| 281 | + ans = (ans + dfs(j)) % mod |
| 282 | + } |
| 283 | + } |
| 284 | + f[i] = ans |
| 285 | + return ans |
| 286 | + } |
| 287 | + return dfs(1) |
| 288 | +} |
72 | 289 | ```
|
73 | 290 |
|
74 | 291 | ### **...**
|
|
0 commit comments