/* * Demo program that uses the Stanford Graphbase and a current * routes graphbase to find the shortest path between fixes/airports. */ #include #include #include #include #include static const char USAGE[] = "Usage: shortest_path {route.gb filename} {from fix} {to fixes}...\n"; static void show_dijkstra_result(Vertex *f); int main(int argc, char **argv) { Graph *g; Vertex *f; int i; if (argc < 4) { fprintf(stderr, USAGE); exit(EXIT_FAILURE); } g = restore_graph(argv[1]); if (g == NULL) { fprintf(stderr, "Cannot restore graph '%s'\n", argv[1]); exit(EXIT_FAILURE); } hash_setup(g); f = hash_out(argv[2]); if (f == NULL) { fprintf(stderr, "Cannot find fix/airport '%s'\n", argv[2]); exit(EXIT_FAILURE); } printf("%s\n", f->name); for (i = 3; i < argc; i++) { Vertex *t = hash_out(argv[i]); int l; if (t == NULL) { fprintf(stderr, "Cannot find fix/airport '%s'\n", argv[i]); exit(EXIT_FAILURE); } l = dijkstra(f, t, g, NULL); show_dijkstra_result(t); f = t; } exit(EXIT_SUCCESS); return 0; } static void show_dijkstra_result(Vertex *f) { Vertex *t = NULL, *p = f; if (p->backlink == NULL) { fprintf(stderr, "No route to '%s'\n", p->name); exit(EXIT_FAILURE); } do { Vertex *q = p->backlink; p->backlink = t; t = p; p = q; } while (t != p); while (t->backlink) { Arc *a = t->arcs; while (a != NULL && a->tip != t->backlink) a = a->next; printf(" %-16s %10.1f n.m.\n", a->a.S, a->len / 60.0); printf("%s\n", a->tip->name); t = t->backlink; } t = p; do { Vertex *q = t->backlink; t->backlink = p; p = t; t = q; } while (p != f); }