Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 5 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Dec 23, 2024
Merged
Changes from 1 commit
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
Next Next commit
添加 0095.城市间货物运输II python3 SPFA版本
  • Loading branch information
SWJTUHJF committed Nov 12, 2024
commit 4ddbb265e47e79919ad501c087919f99a4615612
48 changes: 48 additions & 0 deletions problems/kamacoder/0095.城市间货物运输II.md
View file Open in desktop
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

AltStyle によって変換されたページ (->オリジナル) /