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 ecf70c6

Browse files
committed
feat: 96. 城市间货物运输 III增加python解法
1 parent e7c8d01 commit ecf70c6

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

‎problems/kamacoder/0096.城市间货物运输III.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,42 @@ public class Main {
703703
```
704704

705705
### Python
706+
```python
707+
def main():
708+
# 輸入
709+
n, m = map(int, input().split())
710+
edges = list()
711+
for _ in range(m):
712+
edges.append(list(map(int, input().split() )))
713+
714+
start, end, k = map(int, input().split())
715+
min_dist = [float('inf') for _ in range(n + 1)]
716+
min_dist[start] = 0
717+
718+
# 只能經過k個城市,所以從起始點到中間有(k + 1)個邊連接
719+
# 需要鬆弛(k + 1)次
720+
721+
for _ in range(k + 1):
722+
update = False
723+
min_dist_copy = min_dist.copy()
724+
for src, desc, w in edges:
725+
if (min_dist_copy[src] != float('inf') and
726+
min_dist_copy[src] + w < min_dist[desc]):
727+
min_dist[desc] = min_dist_copy[src] + w
728+
update = True
729+
if not update:
730+
break
731+
# 輸出
732+
if min_dist[end] == float('inf'):
733+
print('unreachable')
734+
else:
735+
print(min_dist[end])
736+
737+
738+
739+
if __name__ == "__main__":
740+
main()
741+
```
706742

707743
### Go
708744

0 commit comments

Comments
(0)

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