한 정점에서 다른 모든 정점까지의 최단 거리를 구하는 알고리즘
우선순위 큐를 활용한 다익스트라 구현
vector<pair<int,int>> graph[N]; // 도착 - 가중치 정보를 담은 그래프
vector<int> DP(N,INT_MAX); // 시작점 s 로부터의 최단 거리를 담은 배열
void dijkstra(int s)
{
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
pq.push({0,s});
DP[s] = 0;
while(pq.size())
{
int cnt = pq.top().first;
int cur = pq.top().second;
pq.pop();
for(int i = 0; i < graph[cur].size(); ++i)
{
int next = graph[cur][i].first;
int weight = graph[cur][i].second;
if(DP[next] > cnt + weight)
{
DP[next] = cnt + weight;
pq.push({DP[next],next});
}
}
}
}
시간 복잡도는 O((V+E)log V) 로 V 는 정점의 개수, E 는 간선의 개수이다.