Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: rename code params #42

Merged
merged 1 commit into from
Mar 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions _posts/2025-02-16-dijkstra-priority-queue.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ C++을 이용하여 예시 코드를 작성하였다. 아래 링크에서 전체

```c++
struct Node {
int vertex; // vertex number(id)
int id; // vertex number(id)
int cost; // cost to reach the vertex
bool operator>(const Node& other) const { // for min-heap
return this->cost > other.cost;
Expand All @@ -92,37 +92,34 @@ struct Node {
```c++
vector<int> dijkstra(int start, int nVertex, vector<vector<Node>>& graph) {
priority_queue<Node, vector<Node>, greater<Node>> pq;
vector<int> costs(nVertex, numeric_limits<int>::max());
vector<int> costs(nVertex, INF);
costs[start] = 0;
pq.push({start, 0});

while (!pq.empty()) {
Node cur = pq.top();
pq.pop();
int vertex = cur.vertex;
int id = cur.id;
int cost = cur.cost;

// Skip if we have already found a better path
if (cost > costs[vertex]) continue;
if (cost > costs[id]) continue;

// Traverse all neighbors
for (const Node& neighbor : graph[vertex]) {
int v = neighbor.vertex;
int c = neighbor.cost;

// Calculate the new cost to reach the neighbor
int nc = costs[vertex] + c;
for (const Node& neighbor : graph[id]) {
int nid = neighbor.id;
int ncost = neighbor.cost;
int new_cost = costs[id] + ncost;

// Update the cost if we have found a better path
if (nc < costs[v]) {
costs[v] = nc;
pq.push({v, nc});
if (new_cost < costs[nid]) {
costs[nid] = new_cost;
pq.push({nid, new_cost});
}
}
}
return costs;
}

```

#### main
Expand Down
Loading