#include <string> #include <vector> #include <queue> #include <iostream> using namespace std; vector<pair<int, int>> map[20010]; int d[20010]; void dijkstra(int start) { priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; pq.push({0, start}); d[start] = 0; while(!pq.empty()) { int now = pq.top().second; int nowCost = pq.top().first; pq.pop(); if(d[now] != nowCost) { continue; } for(int i = 0; i < map[now].size(); i++) { int next = map[now][i].first; int nextCost = map[now][i].second; if(d[next] > nowCost + nextCost) { d[next] = nowCost + nextCost; pq.push({d[next], next}); } } } } int solution(int n, vector<vector<int>> edge) { for(int i = 1; i <= n; i++) { d[i] = 99999999; } for(int i = 0; i < edge.size(); i++) { map[edge[i][0]].push_back({edge[i][1], 1}); map[edge[i][1]].push_back({edge[i][0], 1}); } dijkstra(1); int Max = 0; for(int i = 1; i <= n; i++) { if(Max < d[i]) { Max = d[i]; } } int cnt = 0; for(int i = 1; i <= n; i++) { if(Max == d[i]) { cnt++; } } return cnt; } | cs |
'Programmers > Level 3' 카테고리의 다른 글
[프로그래머스 3] 매칭 점수 (C/C++) (0) | 2020.03.01 |
---|---|
[프로그래머스 3] 순위 (C/C++) (★★) (0) | 2020.02.05 |
[프로그래머스 3] 입국 심사 (C/C++) (0) | 2020.01.27 |
[프로그래머스 3] 예산 (C/C++) (0) | 2020.01.26 |
[프로그래머스 3] 등굣길 (C/C++) (0) | 2020.01.14 |