Skip to content

Instantly share code, notes, and snippets.

@z11i
Last active April 11, 2016 05:12
Show Gist options
  • Select an option

  • Save z11i/77093ca6b45f1b51fb23 to your computer and use it in GitHub Desktop.

Select an option

Save z11i/77093ca6b45f1b51fb23 to your computer and use it in GitHub Desktop.
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