Skip to content

Commit

Permalink
Merge pull request youngyangyang04#2830 from SWJTUHJF/master
Browse files Browse the repository at this point in the history
增添了 0095.城市间货物运输II 和 0096.城市间货物运输III 的Python3 SPFA解法
  • Loading branch information
youngyangyang04 authored Dec 23, 2024
2 parents 21a3215 + 6c497c6 commit 281ff3e
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
48 changes: 48 additions & 0 deletions problems/kamacoder/0095.城市间货物运输II.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ public class Main {

### Python

Bellman-Ford方法求解含有负回路的最短路问题

```python
import sys

Expand Down Expand Up @@ -388,6 +390,52 @@ if __name__ == "__main__":

```

SPFA方法求解含有负回路的最短路问题

```python
from collections import deque
from math import inf

def main():
n, m = [int(i) for i in input().split()]
graph = [[] for _ in range(n+1)]
min_dist = [inf for _ in range(n+1)]
count = [0 for _ in range(n+1)] # 记录节点加入队列的次数
for _ in range(m):
s, t, v = [int(i) for i in input().split()]
graph[s].append([t, v])

min_dist[1] = 0 # 初始化
count[1] = 1
d = deque([1])
flag = False

while d: # 主循环
cur_node = d.popleft()
for next_node, val in graph[cur_node]:
if min_dist[next_node] > min_dist[cur_node] + val:
min_dist[next_node] = min_dist[cur_node] + val
count[next_node] += 1
if next_node not in d:
d.append(next_node)
if count[next_node] == n: # 如果某个点松弛了n次,说明有负回路
flag = True
if flag:
break

if flag:
print("circle")
else:
if min_dist[-1] == inf:
print("unconnected")
else:
print(min_dist[-1])


if __name__ == "__main__":
main()
```

### Go

### Rust
Expand Down
45 changes: 45 additions & 0 deletions problems/kamacoder/0096.城市间货物运输III.md
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,9 @@ public class SPFAForSSSP {


### Python

Bellman-Ford方法求解单源有限最短路

```python
def main():
# 輸入
Expand Down Expand Up @@ -855,6 +858,48 @@ def main():



if __name__ == "__main__":
main()
```

SPFA方法求解单源有限最短路

```python
from collections import deque
from math import inf


def main():
n, m = [int(i) for i in input().split()]
graph = [[] for _ in range(n+1)]
for _ in range(m):
v1, v2, val = [int(i) for i in input().split()]
graph[v1].append([v2, val])
src, dst, k = [int(i) for i in input().split()]
min_dist = [inf for _ in range(n+1)]
min_dist[src] = 0 # 初始化起点的距离
que = deque([src])

while k != -1 and que:
visited = [False for _ in range(n+1)] # 用于保证每次松弛时一个节点最多加入队列一次
que_size = len(que)
temp_dist = min_dist.copy() # 用于记录上一次遍历的结果
for _ in range(que_size):
cur_node = que.popleft()
for next_node, val in graph[cur_node]:
if min_dist[next_node] > temp_dist[cur_node] + val:
min_dist[next_node] = temp_dist[cur_node] + val
if not visited[next_node]:
que.append(next_node)
visited[next_node] = True
k -= 1

if min_dist[dst] == inf:
print("unreachable")
else:
print(min_dist[dst])


if __name__ == "__main__":
main()
```
Expand Down

0 comments on commit 281ff3e

Please sign in to comment.