#include <iostream> #include <algorithm> #include <vector> #include <queue> #include <climits> using namespace std; vector<pair<int, int>> graph[20001]; int n,m; vector<int> dijkstra(int start, int V) { vector<int> dist(V, INT_MAX); dist[start] = 0; priority_queue<pair<int, int>> pq; pq.push(make_pair(0, start)); while(!pq.empty()) { int cost = -pq.top().first; int here = pq.top().second; pq.pop(); if(dist[here] < cost) continue; for(int i=0;i<graph[here].size();i++) { int nextPos = graph[here][i].first; int nextDist = cost + graph[here][i].second; if(dist[nextPos] > nextDist) { dist[nextPos] = nextDist; pq.push(make_pair(-nextDist, nextPos)); } } } return dist; } int main() { cin.tie(NULL); ios::sync_with_stdio(false); cin >> n >> m; n++; while(m--) { int from, to; cin >> from >> to; graph[from].push_back(make_pair(to, 1)); graph[to].push_back(make_pair(from, 1)); } vector<int> vv = dijkstra(1, n); int ans = 0, ansNum = n, gaesu = 0; for(int i=n-1;i>=2;i--) { if(ans <= vv[i]) { ans = vv[i]; ansNum = i; } } for(int i=2;i<n;i++) if(ans == vv[i]) gaesu++; cout << ansNum << " " << ans << " " << gaesu; }
다익스트라 문제입니다.
서로 연결된 정점들을 입력받고 그 간선의 길이를 1이라고 두고, 1번 정점으로부터 가장 먼 정점의 번호와 그 길이, 그리고 먼 정점으로터의 거리와 같은 다른 정점들을 출력하면 되겠습니다.
다익스트라 문제를 다시금 복습하게 해주는 문제였습니다.
'Algorithm > Baekjoon & Algospot' 카테고리의 다른 글
[C++] BOJ 1309 - 동물원 (0) | 2018.07.23 |
---|---|
[C++] Algospot PI - 원주율 외우기 (0) | 2018.07.21 |
[C++] BOJ 1012 - 유기농 배추 (0) | 2018.07.17 |
[C++] BOJ 7785 - 회사에 있는 사람 (0) | 2018.07.16 |
[C++] BOJ 13016 - 내 왼손에는 흑염룡이 잠들어 있다 (0) | 2018.07.15 |