Implementation of Dijkstra algorithm
The graph is actually an adjacency graph. This is a much better name than graph to avoid people confusing it with a grid of nodes.

The next node should be the unvisited node with the lowest cost.

for (int k = 0 ; k &lt; graph.length ; k++) {
    if (!visited[k] &amp;&amp; result[k] &lt; nextNodeCost) {
        nextNode = k;
        nextNodeCost = result[k];
    }
}


This way you avoid needing to visit nodes multiple times because you will then never change a visited node's cost.

The result array should be initialized with result[0] = 0; then you can remove the check in the for loop.