Last active
April 11, 2016 05:12
-
-
Save z11i/77093ca6b45f1b51fb23 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void dijkstra() { | |
| int[] dist = new int[n + 1]; | |
| boolean[] visited = new boolean[n + 1]; | |
| for (int source = 0; source < k + 1; source++) { | |
| arrays.fill(dist, INFINITY); | |
| arrays.fill(visited, false); | |
| dist[list[source]] = 0; | |
| for (int t = 0; t < n; t++) { | |
| int min = INFINITY; | |
| int w = -1; | |
| for (int v = 0; v < n + 1; v++) { // find vertex with least cost | |
| if (!visited[v] && dist[v] <= min) { | |
| min = dist[v]; | |
| w = v; | |
| } | |
| } | |
| visited[w] = true; | |
| for (int vert = 0; vert < n + 1; vert++) { // relax | |
| if (!visited[vert] && | |
| dist[w] != inf && | |
| dist[vert] > dist[w] + t[w][vert]) { | |
| dist[vert] = dist[w] + t[w][vert]; | |
| } | |
| } | |
| for (int i = 0; i < n + 1; i++) { | |
| t[list[source]][i] = dist[i]; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment