Skip to content

Instantly share code, notes, and snippets.

@rigibun
Last active August 29, 2015 14:01
Show Gist options
  • Save rigibun/57cf2411730409af0b1c to your computer and use it in GitHub Desktop.
Save rigibun/57cf2411730409af0b1c to your computer and use it in GitHub Desktop.
#include "dijkstra.h"
void dijkstra(int32_t *d, size_t V, Edge *es, size_t E, size_t s) {
const int INF = INT32_MAX;
bool used[V];
for(size_t i = 0; i < V; i++) {
used[i] = false;
d[i] = INF;
}
d[s] = 0;
while(true) {
bool continueFlag = false;
size_t v = V;
for(int i = 0; i < V; i++) {
if(!used[i] && (v == V || d[i] < d[v])) {
v = i;
continueFlag = true;
}
}
if(!continueFlag)
break;
used[v] = true;
for(int i = 0; i < E; i++) {
if(es[i].from == v) {
Edge e = es[i];
if(d[e.from] + e.cost < d[e.to])
d[e.to] = d[e.from] + e.cost;
}
}
}
}
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#ifndef DIJKSTRA_H
#define DIJKSTRA_H
typedef struct _Edge {
size_t from, to;
int32_t cost;
} Edge;
void dijkstra(int32_t *d, size_t V, Edge *es, size_t E, size_t s);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment