BOJ

백준 18352 - 특정 거리의 도시 찾기

yanJuicy 2021. 7. 19. 12:52
반응형

문제

https://www.acmicpc.net/problem/18352

 

18352번: 특정 거리의 도시 찾기

첫째 줄에 도시의 개수 N, 도로의 개수 M, 거리 정보 K, 출발 도시의 번호 X가 주어진다. (2 ≤ N ≤ 300,000, 1 ≤ M ≤ 1,000,000, 1 ≤ K ≤ 300,000, 1 ≤ X ≤ N) 둘째 줄부터 M개의 줄에 걸쳐서 두 개

www.acmicpc.net

 

 

풀이

 

모든 간선의 비용이 동일하기 때문에 bfs 탐색을 통해 최단 거리를 찾을 수 있다. 시작점 x에서부터 bfs 탐색을 통해 모든 도시까지의 최단 거리를 구한 후에 k 값과 비교하여 답을 구한다. 

 

 

코드

 

java

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
 
import java.util.*;
 
public class Main {
 
    static Scanner sc = new Scanner(System.in);
    static List<List<Integer>> graph = new ArrayList<>();
    static int[] distance;
    static boolean[] visited;
 
    public static void main(String[] args) {
        int n, m, k, x;
 
        n = sc.nextInt();
        m = sc.nextInt();
        k = sc.nextInt();
        x = sc.nextInt();
 
        distance = new int[n + 1];
        visited = new boolean[n + 1];
        for (int i=0; i<=n; i++) {
            graph.add(new ArrayList<>());
        }
 
        for (int i=0; i<m; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            graph.get(a).add(b);
        }
 
        List<Integer> result = solve(n, k, x);
        for (int i : result) {
            System.out.println(i);
        }
    }
 
    private static List<Integer> solve(int n, int k, int x) {
        List<Integer> result = new ArrayList<>();
 
        bfs(x);
        boolean check = false;
        for (int i=1; i<=n; i++) {
            if (distance[i] == k) {
                result.add(i);
                check = true;
            }
        }
 
        if (!check) {
            result.add(-1);
        }
 
        return result;
    }
 
    private static void bfs(int x) {
        Queue<Integer> q = new LinkedList<>();
        q.add(x);
        visited[x] = true;
 
        while (!q.isEmpty()) {
            int cur = q.poll();
 
            for (int n : graph.get(cur)) {
                if (!visited[n]) {
                    q.add(n);
                    visited[n] = true;
                    distance[n] = distance[cur] + 1;
                }
            }
        }
    }
}
 
cs

 

python

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
29
30
31
32
33
34
35
n, m, k, x = map(int, input().split())
 
graph = [[] for _ in range(n + 1)]
visited = [False* (n + 1)
distance = [0* (n + 1)
 
for i in range(m):
    a, b = map(int, input().split())
    graph[a].append(b)
 
from collections import deque
 
def bfs(s):
    q = deque()
    q.append(s)
    visited[s] = True
 
    while q:
        cur = q.popleft()
 
        for n in graph[cur]:
            if not visited[n]:
                visited[n] = True
                q.append(n)
                distance[n] = distance[cur] + 1
    
bfs(x)
check = False
for i in range(1, n+1):
    if distance[i] == k:
        print(i)
        check = True
 
if not check: 
    print(-1)
cs

 

 

 

반응형

'BOJ' 카테고리의 다른 글

백준 11779 - 최소비용 구하기 2  (0) 2021.08.12
백준 1916 - 최소비용 구하기  (0) 2021.08.11
백준 1439 - 뒤집기  (0) 2021.07.18
백준 1793 - 타일링  (0) 2021.06.19
백준 2805 - 나무 자르기  (0) 2021.06.18