Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 백준
- dfs
- 구현
- C/C++ 구현
- 해시구현
- 자료구조
- 시간 복잡도
- 5397
- 게리멘더링2
- 원판 돌리기
- Stack 이란
- 백준 17779
- 백준 2447
- 백준 17471
- 스택의 특징
- 버킷 정렬
- 조세퍼스 순열
- 백준 17822
- AVL 시간 복잡도
- heap
- c#
- 1764
- 해시 구현
- ㅣ풀이
- 풀이
- 백준 1406
- 백준 5397
- 별 찍기 10
- qorwns
- 백준 1158
Archives
- Today
- Total
홍시홍의 프로그래밍
[백준 6118] 숨바꼭질 본문
https://www.acmicpc.net/problem/6118
6118번: 숨바꼭질
문제 재서기는 수혀니와 교외 농장에서 숨바꼭질을 하고 있다. 농장에는 헛간이 많이 널려있고 재서기는 그 중에 하나에 숨어야 한다. 헛간의 개수는 N(2 <= N <= 20,000)개이며, 1 부터 샌다고 하자. 재서기는 수혀니가 1번 헛간부터 찾을 것을 알고 있다. 모든 헛간은 M(1<= M <= 50,000)개의 양방향 길로 이어져 있고, 그 양 끝을 A_i 와 B_i(1<= A_i <= N; 1 <= B_i <= N; A_i != B_i)로 나타낸
www.acmicpc.net
요구사항
시작 정점에서 부터 제일 거리가 먼 정점의 길이 구하기
풀이
다익스트라 풀이
시작 정점의 거리는 1로 초기화하는게 아니라 0으로 초기화 한다
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
#define p pair<int,int>
struct go {
int x;
int y;
};
bool com(go a, go b) {
if (a.x > b.x) {
return true;
}
else if (a.x == b.x) {
if (a.y < b.y) {
return true;
}
}
return false;
}
bool com1(go a, go b) {
if (a.x > b.x) {
return true;
}
return false;
}
go dist[20020];
vector<int> v[20020];
int n, m;
int ans;
vector<p> vans;
int visit[20020];
int main(){
scanf("%d%d", &n, &m);
for (int i = 1; i <= 20019; i++)
{
dist[i].x = 987654321;
dist[i].y = i;
}
for (int i = 0; i < m; i++) {
int x, y;
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
}
priority_queue<pair<int, int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
q.push({ 0,1 });
visit[1] = 1;
dist[1].x = 0;
while (!q.empty()) {
int nowdist = q.top().first;
int nownode = q.top().second;
q.pop();
if (dist[nownode].x < nowdist) continue;
for (int i = 0; i < v[nownode].size(); i++) {
int nextvalue = v[nownode].at(i);
int nextdist = nowdist + 1;
if (dist[nextvalue].x > nextdist) {
dist[nextvalue].x = nextdist;
q.push({ nextdist,nextvalue });
}
}
}
//cout << "A" << endl;
int ansresult = 1;
int anscnt = 1;
//sort(dist + 1, dist + n + 1, com1);
for (int i = 2; i <= n; i++) {
//cout << dist[i].x << " ";
if (dist[ansresult].x < dist[i].x) {
//cout << "B" << endl;
anscnt = 1;
ansresult = i;
}
else if (dist[ansresult].x == dist[i].x) {
anscnt++;
//cout << "A" << endl;
//cout << anscnt << endl;
}
}
cout << ansresult << " " << dist[ansresult].x << " " << anscnt << "\n";
}
'알고리즘 문제풀이 > 백준' 카테고리의 다른 글
[백준 10282] 해킹 (0) | 2020.03.04 |
---|---|
[백준 2983] 개구리 공주 (0) | 2020.03.04 |
[백준 1620] 나는야 포켓몬 마스터 (0) | 2020.03.02 |
[백준 10774] 저지 (0) | 2020.03.02 |
[백준 4343] Arctic Network (0) | 2020.03.02 |
Comments