Skip to content

Instantly share code, notes, and snippets.

@tiger1710
Last active June 8, 2020 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiger1710/1f966979e20df324292f3dfe3dde3364 to your computer and use it in GitHub Desktop.
Save tiger1710/1f966979e20df324292f3dfe3dde3364 to your computer and use it in GitHub Desktop.
Dijkstra
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