Python 广度优先搜索:最短到达hackerrank
给定一个由N个节点(标记为1到N)组成的无向图,其中一个节点S表示起始位置,任意两个节点之间的边在图中的长度为6个单位。问题 需要计算从起始位置(节点S)到图形中所有其他节点的最短距离 解决方案:这显然是floyd算法在最小距离上的应用 我尝试的内容:我尝试了下面的代码,它通过了2个测试用例,但在所有其他测试用例中都失败了。我对那只鬼鬼祟祟的虫子束手无策。我只想得到解决办法的提示。就复杂性而言,提供其他解决方法的提示是很好的,但我正在寻找当前代码中的一个潜在错误Python 广度优先搜索:最短到达hackerrank,python,algorithm,shortest-path,floyd-warshall,Python,Algorithm,Shortest Path,Floyd Warshall,给定一个由N个节点(标记为1到N)组成的无向图,其中一个节点S表示起始位置,任意两个节点之间的边在图中的长度为6个单位。问题 需要计算从起始位置(节点S)到图形中所有其他节点的最短距离 解决方案:这显然是floyd算法在最小距离上的应用 我尝试的内容:我尝试了下面的代码,它通过了2个测试用例,但在所有其他测试用例中都失败了。我对那只鬼鬼祟祟的虫子束手无策。我只想得到解决办法的提示。就复杂性而言,提供其他解决方法的提示是很好的,但我正在寻找当前代码中的一个潜在错误 def short_paths(
def short_paths(cost, nodes):
for i in range(1, nodes):
for j in range(1, nodes):
for k in range(1, nodes):
if cost[i][j] > cost[i][k]+cost[k][j]:
cost[i][j] = cost[i][k]+cost[k][j]
return cost
tests = int(input())
while tests:
x = input().split(" ")
nodes, edges = int(x[0]), int(x[1])
#initialize everything with infinity
dp = [[1<<31 for i in range(nodes+1)] for i in range(nodes+1)]
#distance between self is 0
for i in range(nodes+1):
dp[i][i] = 0
while edges:
p = input().split(" ")
x, y = int(p[0]), int(p[1])
#undirected graph
dp[x][y] = 6
dp[y][x] = 6
edges -= 1
src = int(input())
dp = short_paths(dp, nodes+1)
result = []
for i in range(1, nodes+1):
if src != i:
if dp[src][i] == 1<<31:
result.append("-1")
else:
result.append(dp[src][i])
print(" ".join(str(e) for e in result))
tests -= 1
def短路径(成本、节点):
对于范围内的i(1,节点):
对于范围内的j(1,节点):
对于范围内的k(1个节点):
如果成本[i][j]>成本[i][k]+成本[k][j]:
成本[i][j]=成本[i][k]+成本[k][j]
退货成本
tests=int(输入())
测试时:
x=input().split(“”)
节点,边=int(x[0]),int(x[1])
#用无限初始化一切
dp=[[1我认为在这些方面存在问题:
for i in range(1, nodes):
for j in range(1, nodes):
for k in range(1, nodes):
为了使结果正确,应首先迭代k:
尝试:
由于DP使用了以前的结果,因此迭代的顺序对于获得正确的结果至关重要
我记忆顺序的方式是认为算法的第k次迭代只使用位置1到k的中间节点计算从I到j的最短路径
但是,对于这个问题,这个O(N^3)方法将超时。更好的方法是从起始位置执行广度优先搜索,其复杂性将改为N+M
import queue
def BFS(s):
q = queue.Queue()
q.put(s)
visited[s] = True
dist[s] = 0
while not q.empty():
u = q.get()
for v in graph[u]:
if not visited[v]:
visited[v] = True
q.put(v)
dist[v] = dist[u] + 1
Q = int(input())
for _ in range(Q):
n, m = map(int, input().split())
graph = [[] for i in range(n)]
visited = [False for i in range(n)]
dist = [-1 for i in range(n)]
for i in range(m):
u, v = map(lambda x: int(x) - 1, input().split())
graph[u].append(v)
graph[v].append(u)
s = int(input()) - 1
BFS(s)
for i in range(n):
if i == s:
continue
print(dist[i]*6 if dist[i] != -1 else '-1', end = ' ')
print()
只需使用普通BFS不,检查我传递给此函数的值。你是对的-我已经提出了一个替代的修正方案,抱歉,但我担心,这有什么不同?是的,但我没有超时。只是答案不匹配。我还是会使用BFS,但只是出于好奇,使用Floy的当前解决方案有什么问题d?我相信唯一的问题是迭代的顺序。你试过按我建议的顺序使用变量吗?你为什么要尝试使用Floyd Warshall?这是加权图中所有对最短路径问题的解决方案,其中边可以有不同的权重,边权重甚至可以为负。这完全是过火了而且速度非常慢。问题甚至在标题中提到了广度优先搜索;这是使用广度优先搜索的一个明确提示。(此外,链接中的问题给所有边的权重为6,而不是66。)感谢提示。我将使用它,但我想知道为什么现有的解决方案无法提供答案。你在哪里看到66?你的问题是“任意两个节点之间的边的长度为66个单位”。链接显示了不同的内容。@user2357112已修改。谢谢。节点+1
的所有出现是怎么回事?
import queue
def BFS(s):
q = queue.Queue()
q.put(s)
visited[s] = True
dist[s] = 0
while not q.empty():
u = q.get()
for v in graph[u]:
if not visited[v]:
visited[v] = True
q.put(v)
dist[v] = dist[u] + 1
Q = int(input())
for _ in range(Q):
n, m = map(int, input().split())
graph = [[] for i in range(n)]
visited = [False for i in range(n)]
dist = [-1 for i in range(n)]
for i in range(m):
u, v = map(lambda x: int(x) - 1, input().split())
graph[u].append(v)
graph[v].append(u)
s = int(input()) - 1
BFS(s)
for i in range(n):
if i == s:
continue
print(dist[i]*6 if dist[i] != -1 else '-1', end = ' ')
print()