#include <stdio.h> #include <iostream> #include <vector> #include <queue> #include <algorithm> #include <map> using namespace std; int N, M; vector<pair<int, int>> v[20010]; int d[20010]; int Max; int MaxIdx; int MaxNum; void dijkstra() { priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; pq.push({0, 1}); // {비용, 정점} d[1] = 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 < v[now].size(); i++) { int next = v[now][i].first; int nextCost = v[now][i].second; if(d[next] > d[now] + nextCost) { d[next] = d[now] + nextCost; pq.push({d[next], next}); } } } } int main(void) { // freopen("B6118_input.txt", "r", stdin); cin >> N >> M; for(int i = 2; i <= N; i++) { d[i] = 99999999; } for(int i = 1; i <= M; i++) { int from, to; cin >> from >> to; // 양방향 삽입 v[from].push_back({to, 1}); v[to].push_back({from, 1}); } dijkstra(); for(int i = 2; i <= N; i++) { if(d[i] > Max) { Max = d[i]; MaxIdx = i; } } for(int i = 2; i <= N; i++) { if(Max == d[i]) { MaxNum++; } } cout << MaxIdx << " " << Max << " " << MaxNum; return 0; } | cs |
'Baekjoon > Graph' 카테고리의 다른 글
[백준 5719] 거의 최단경로 (Dijkstra) (C/C++) (★★★) (0) | 2020.02.19 |
---|---|
[백준 16681] 등산 (Dijkstra) (C/C++) (★★) (0) | 2020.02.18 |
[백준 2211] 네트워크 복구 (Dijkstra) (C/C++) (0) | 2020.02.18 |
[백준 1504] 특정한 최단경로 (Floyd-Warshall) (C/C++) (0) | 2020.02.17 |
[백준 1238] 파티 (Floyd-Warshall) (C/C++) (0) | 2020.02.17 |