-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from vmmc2/master
Implement Dijkstra path-finding (adjacency list, w/ small local optimizations) in C++
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include <bits/stdc++.h> | ||
#define INF 9999999 | ||
#define MAXN 100000 | ||
|
||
using namespace std; | ||
|
||
typedef pair<int,int> pii; | ||
vector<pii> adjlist[MAXN]; | ||
int distancias[MAXN]; | ||
|
||
void Dijkstra(int s){ | ||
for(int i = 0; i < MAXN; i++){ | ||
distancias[i] = INF; | ||
} | ||
distancias[s] = 0; | ||
priority_queue< pii, vector<pii>, greater<pii> >heap; | ||
heap.push({0,s}); | ||
while(!heap.empty()){ | ||
int dist, atualvertex; | ||
dist = heap.top().first; | ||
atualvertex = heap.top().second; | ||
heap.pop(); | ||
if(dist > distancias[atualvertex]){ | ||
continue; | ||
} | ||
for(int i = 0; i < (int)adjlist[atualvertex].size(); i++){ | ||
int dist2, proxvertex; | ||
dist2 = adjlist[atualvertex][i].first; | ||
proxvertex = adjlist[atualvertex][i].second; | ||
if(distancias[proxvertex] > distancias[atualvertex] + dist2){ | ||
distancias[proxvertex] = distancias[atualvertex] + dist2; | ||
heap.push({distancias[proxvertex], proxvertex}); | ||
} | ||
} | ||
} | ||
} |