https://school.programmers.co.kr/learn/courses/30/lessons/12978
#include <vector>
#include <queue>
using namespace std;
vector<pair<int,int>> V[55];
vector<int> Dist;
int Min(int A, int B) { if (A < B) return A; return B; }
void Dijkstra(int N)
{
priority_queue<pair<int, int>> PQ;
PQ.push(make_pair(0, 1));
Dist[1] = 0;
while (PQ.empty() == 0)
{
int Cost = -PQ.top().first;
int Cur = PQ.top().second;
PQ.pop();
for (int i = 0; i < V[Cur].size(); i++)
{
int Next = V[Cur][i].first;
int nCost = V[Cur][i].second;
if (Dist[Next] > Cost + nCost)
{
Dist[Next] = Cost + nCost;
PQ.push(make_pair(-Dist[Next], Next));
}
}
}
}
int solution(int N, vector<vector<int> > road, int K)
{
Dist.resize(N + 1, 2e9);
for (int i = 0; i < road.size(); i++)
{
int N1 = road[i][0];
int N2 = road[i][1];
int Dist = road[i][2];
V[N1].push_back(make_pair(N2, Dist));
V[N2].push_back(make_pair(N1, Dist));
}
Dijkstra(N);
int answer = 0;
for (int i = 1; i <= N; i++)
{
if (Dist[i] <= K) answer++;
}
return answer;
}
'Coding Test > Programmers' 카테고리의 다른 글
[Programmers] (힙(Heap)) Lv 2. 더 맵게 (0) | 2024.10.15 |
---|---|
[Programmers] (2017 팁스타운) Lv 2. 짝지어 제거하기 (1) | 2024.10.15 |
[Programmers] Lv 2. 줄 서는 방법 (0) | 2023.11.16 |
[Programmers] (2021 Dev-Matching: 웹 백엔드 개발자(상반기)) Lv 2. 행렬 테두리 회전하기 (0) | 2023.11.14 |
[Programmers] (그리디 or 완전탐색) Lv 2. 마법의 엘리베이터 (0) | 2023.11.10 |
댓글