Skip to content

Conversation

@lthero-big
Copy link

修正卡码网0095.城市间货物运输II中,Python解决SPFA方法求解含有负回路的最短路问题的错误

原代码(错误的)

                count[next_node] += 1
                if next_node not in d:
                      d.append(next_node)

在输入例子

4 6
1 4 3
1 2 1
1 3 1
3 2 -2
2 4 1
3 4 0

会输出circle,但正确结果是0

纠正代码(可以通过全部例子)

                if next_node not in d:
                      count[next_node] += 1
                      d.append(next_node)

修正卡码网0095.城市间货物运输II中,Python解决SPFA方法求解含有负回路的最短路问题的错误

count[next_node] += 1应该在 if next_node not in d: 语句块内
@shengjiex98
Copy link

除了之前count[next_node] += 1的缩进错误,因为链表的线性读取特性,if not in_queue[next_node]的判断每次需要 O(n) 时间复杂度。这里应该用其他解法中提过的in_queue数组/列表做判断 (O(1))。
修复PR: lthero-big#1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants