#include #include #include "Graph.h" #include "Hash.h" #include "Maze.h" #include "Heap.h" #include "test_utilities.h" HashTable Dijkstras(const Digraph &dig, int v, const HashTable > & edge_weights); // defined at bottom of file // Given a Maze m, a vertex id v, and a HashTable of vertex weights, // return a HashTable H such that for each vertex w reachable from v, // H[w] is the minimum, over all paths from v to w, of the total // weight of the vertices on the path. HashTable find_vertex_shortest_paths(const Graph &g, int v, const HashTable & vertex_weights) { // YOUR CODE HERE } int main() { Maze m; // read the maze in text format from stdin std::cin >> m; // make up some vertex weights HashTable vertex_weights; const Array vertices = m.graph().vertices(); for (int i = 0; i < vertices.size(); ++i) { vertex_weights[vertices[i]] = 1 + (i*(i+1) % 7); } // find distances from the start vertex HashTable distances; distances = find_vertex_shortest_paths(m.graph(), m.get_start_vertex(), vertex_weights); // output the distances mod 10 for (int i = 0; i < vertices.size(); ++i) { int v = vertices[i]; if (v != m.get_start_vertex() && v != m.get_end_vertex() && distances.exists(v)) { Array loc = m.loc_of_vertex(v); m.set_text_at(loc[0], loc[1], '0' + (distances[v] % 10)); } } std::cout << m << std::endl; // output the distance from start to end if (distances.exists(m.get_end_vertex())) std::cout << "Distance from start to end is " << distances[m.get_end_vertex()] << std::endl; else std::cout << "No path from start to end." << std::endl; } HashTable Dijkstras(const Digraph &dig, int S, const HashTable > & edge_weights) { HashTable distances; Heap Q; Q.insert(S, 0); while (! Q.empty()) { int v; int key; Q.delete_min(v, key); if (distances.exists(v)) continue; distances[v] = key; Array nbrs = dig.out_nbrs(v); for (int i = 0; i < nbrs.size(); ++i) Q.insert(nbrs[i], key + edge_weights[v][nbrs[i]]); } return distances; }