Last active
June 8, 2020 02:11
-
-
Save tiger1710/1f966979e20df324292f3dfe3dde3364 to your computer and use it in GitHub Desktop.
Dijkstra
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
typedef pair<int, int> point; | |
const int INF = 987654321; | |
//정점의 개수 | |
int V; | |
//그래프의 인접리스트 (연결된 정점 번호, 간선 가중치) | |
vector<point> adj; | |
vector<int> dijkstra(const int& src) { | |
vector<int> dist(V, INF); | |
dist[src] = 0; | |
priority_queue<point> pq; | |
pq.push(make_pair(0, src)); | |
while (not pq.empty()) { | |
const int cost = -pq.top().first; | |
const int here = pq.second().second; | |
pq.pop(); | |
if (dist[here] < cost) continue; | |
for (const point& next : adj[here]) { | |
const int nextCost = cost + next.second; | |
const int there = next.first; | |
if (nextCost < dist[there]) { | |
dist[there] = nextCost; | |
pq.push(make_pair(-nextCost, there)); | |
} | |
} | |
} | |
return dist; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment