简体   繁体   English

CodeFights:Dijkstra的算法实现

[英]CodeFights: Dijkstra's Algorithm implementation

Considering this code fight problem: 考虑以下代码冲突问题:

Consider a big city located on n islands. 考虑一个位于n个岛上的大城市。 There are bridges connecting the islands, but they all have only one-way traffic. 有连接岛屿的桥梁,但它们都只有单向交通。 To make matters worse, most of the bridges are closed at night, so there is at most one bridge with traffic going from any island A to any other island B. 更糟糕的是,大多数桥梁在夜间关闭,因此最多只有一座桥梁,交通从任何岛屿A到任何其他岛屿B。

There is a programmer who turns a penny by working nights as an Uber driver. 有一个程序员在夜里以Uber司机的身分赚钱。 One night his phone dies right after he picks up a rider going from island 0 to island (n - 1). 一天晚上,他接载从零岛到零岛(n-1)的骑手后,电话就死了。 He has the map of the city bridges in his laptop though (stored as a matrix of distances), so he decides to implement an algorithm that calculates the shortest path between those two islands and evaluate the cost based on the distance of the path. 尽管他在笔记本电脑中有城市桥梁的地图(存储为距离矩阵),所以他决定实施一种算法,该算法计算这两个岛之间的最短路径并根据路径的距离评估成本。 Assume that each mile of the trip is 1$. 假设行程的每一英里为1美元。

I have decided to implement Dijkstra's algorithm to solve it: 我决定实现Dijkstra的算法来解决它:

def nightRoute(city):
    visited = [];
    visited.append(0);
    distance = [];
    for x in range(0, len(city)):
        distance.append(float("inf"));
    distance[0] = 0;

    while(len(visited) != len(city)):
        for i in visited:
            print visited;
            min = float("inf");
            minNode = -1;
            for j in range(0, len(city)):
                if ( j not in visited and city[i][j] != -1):
            if distance[j] > distance[i] + city[i][j]:
            distance[j] = distance[i] + city[i][j]
                    if distance[i] + city[i][j] <= min:
                        min = distance[i] + city[i][j];
                        minNode = j
            if(min != float("inf") and minNode != -1):
                visited.append(minNode);
    return distance[len(city)-1];

However when I run the code, it only passes 6/7 test cases (the last 3 test cases are hidden to me so i don't know what the inputs are). 但是,当我运行代码时,它仅通过6/7个测试用例(最后3个测试用例对我隐藏了,所以我不知道输入的是什么)。

Can anyone tell me what is wrong with my implementation of dijkstra's? 谁能告诉我我实施dijkstra的问题是什么? Am i missing something in how dijkstras work? 我在dijkstras的工作方式中缺少什么吗?

You should find the visited node contains the minimum key (distance) in each iteration, instead of trying all visited nodes. 您应该找到每次迭代中包含最小键(距离)的访问节点,而不是尝试所有访问节点。 Although the relaxation is still safe, you may put some of the nodes not having the optimal key to the visited set, hence compromises the result. 尽管放宽仍然是安全的,但是您可以将一些没有最佳关键字的节点放到被访问集合中,因此会影响结果。

Furthermore, although I don't think this will change the result, it would be better to avoid comparing two float numbers using == or != . 此外,尽管我认为这不会改变结果,但最好避免使用==!=比较两个浮点数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM