From cd580243d131a77591eb27f463c65693e394bd09 Mon Sep 17 00:00:00 2001 From: chuangtsh Date: Sat, 12 Jul 2025 17:31:32 +0800 Subject: [PATCH 1/2] finish Dijkstra's task --- Dijkstra.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/Dijkstra.cpp b/Dijkstra.cpp index 7d39740..26bf434 100644 --- a/Dijkstra.cpp +++ b/Dijkstra.cpp @@ -29,6 +29,12 @@ using namespace std; +struct CompareDistance { + bool operator()(pair lhs, pair rhs) { + return lhs.second > rhs.second; + } +}; + // Graph representation using adjacency list class Graph { private: @@ -54,7 +60,60 @@ class Graph { // 2. Create arrays for distances and for tracking the path // 3. Initialize all distances as INFINITE and src distance as 0 // 4. Process vertices in order of their distance from src - // 5. Reconstruct and print the shortest path from src to dest + priority_queue, vector>, CompareDistance> pq; + vector visited(V, 0); + vector cost(V, INT32_MAX); + vector path(V, -1); + stack result_path; + pq.push(make_pair(src, 0)); + cost[src] = 0; + + while ( visited[dest] == 0 && pq.size() > 0 ) { + auto cur = pq.top(); + pq.pop(); + visited[cur.first] = true; + // cout << "cur:" << cur.first << endl; + + for ( auto ni : adj[cur.first] ) { + if ( visited[ni.first] ) continue; + + if ( cost[ni.first] > cost[cur.first] + ni.second ) { + cost[ni.first] = cost[cur.first] + ni.second; + path[ni.first] = cur.first; + pq.push(make_pair(ni.first, cost[ni.first])); + // current method would cause duplicate nodes in pqueue, + // to improve is to use set>, since st.begin() is the smallest + // cout << "add neighbor: " << ni.first << "<>" << cost[ni.first] << endl; + } + } + } + + if ( visited[dest] == 0 ) { + cout << "no path exists\n"; + return; + } + + + for ( int it = dest; it != src; it = path[it] ) { + result_path.push(it); + } + result_path.push(src); + + + cout << "Shortest travel time: " << cost[dest] << " hours\n"; + + cout << "Path: "; + + cout << result_path.top(); + result_path.pop(); + while ( !result_path.empty() ) { + int cur = result_path.top(); + result_path.pop(); + cout << " -> " << cur; + } + cout << endl; + + } }; From 638f12a3dc23b833a6b097dcf98ca992c2fb5cde Mon Sep 17 00:00:00 2001 From: chuangtsh Date: Fri, 1 Aug 2025 17:00:47 +0800 Subject: [PATCH 2/2] finish A* task --- AStar.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/AStar.cpp b/AStar.cpp index 120ca76..c3566b5 100644 --- a/AStar.cpp +++ b/AStar.cpp @@ -31,6 +31,13 @@ using namespace std; +// Comparator for priority queue to make it a min-heap +struct CompareF { + bool operator()(pair lhs, pair rhs) { + return lhs.first > rhs.first; // Compare f-scores, smaller f-score has higher priority + } +}; + // Graph representation using adjacency list class Graph { private: @@ -65,6 +72,52 @@ class Graph { // 3. Initialize all g_scores as INFINITE and src g_score as 0 // 4. Process vertices in order of their f_score (not just distance) // 5. Reconstruct and print the shortest path from src to dest + priority_queue, vector>, CompareF> pq; + vector f_score(V, INT32_MAX); + vector g_score(V, INT32_MAX); + vector visited(V, false); + vector path(V, -1); + g_score[src] = 0; + f_score[src] = heuristic[src]; + pq.push(make_pair(f_score[src], src)); + while ( !pq.empty() ) { + int cur = pq.top().second; + if ( cur == dest ) break; + pq.pop(); + for ( auto[nd, weight] : adj[cur] ) { + if ( g_score[nd] > g_score[cur] + weight ) { + g_score[nd] = g_score[cur] + weight; + path[nd] = cur; + } + if ( f_score[nd] > g_score[nd] + heuristic[nd] ) { + f_score[nd] = g_score[nd] + heuristic[nd]; + pq.push(make_pair(f_score[nd], nd)); + } + } + } + if ( path[dest] == -1 ) { + cout << "cannot find path\n"; + return; + } + stack result; + int cur = dest; + while ( 1 ) { + result.push(cur); + if ( cur == src ) break; + cur = path[cur]; + } + cout << "Shortest travel time: " << g_score[dest] << " hours\n"; + cout << "Path: "; + + cout << result.top(); + result.pop(); + while ( !result.empty() ) { + int cur = result.top(); + result.pop(); + cout << " -> " << cur; + } + cout << endl; + } };