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

Commit dc2edca

Browse files
Merge pull request youngyangyang04#2721 from sunlight0602/0094-bellman-ford-python-SPFA
feat: 0094.城市间货物运输I-SPFA python solution
2 parents ff8f26b + e24c307 commit dc2edca

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

‎problems/kamacoder/0094.城市间货物运输I-SPFA.md‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,39 @@ public class Main {
425425
```
426426

427427
### Python
428-
428+
```Python
429+
import collections
430+
431+
def main():
432+
n, m = map(int, input().strip().split())
433+
edges = [[] for _ in range(n + 1)]
434+
for _ in range(m):
435+
src, dest, weight = map(int, input().strip().split())
436+
edges[src].append([dest, weight])
437+
438+
minDist = [float("inf")] * (n + 1)
439+
minDist[1] = 0
440+
que = collections.deque([1])
441+
visited = [False] * (n + 1)
442+
visited[1] = True
443+
444+
while que:
445+
cur = que.popleft()
446+
visited[cur] = False
447+
for dest, weight in edges[cur]:
448+
if minDist[cur] != float("inf") and minDist[cur] + weight < minDist[dest]:
449+
minDist[dest] = minDist[cur] + weight
450+
if visited[dest] == False:
451+
que.append(dest)
452+
visited[dest] = True
453+
454+
if minDist[-1] == float("inf"):
455+
return "unconnected"
456+
return minDist[-1]
457+
458+
if __name__ == "__main__":
459+
print(main())
460+
```
429461
### Go
430462

431463
### Rust

0 commit comments

Comments
(0)

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