Algorithm/Baekjoon

[๋ฐฑ์ค€ 1167] Java - bfs ์—ฌ๋Ÿฌ๋ฒˆ ์‚ฌ์šฉ

say! 2025. 3. 1. 16:36
728x90

๐Ÿ”ตbfs 2๋ฒˆ ํƒ์ƒ‰

1. ์ž„์˜์˜ ์ •์ ์—์„œ bfs > ๊ฑฐ๋ฆฌ ์ œ์ผ ๋จผ๊ฒŒ ๊ฐ€์žฅ ๋์— ์žˆ๋Š” ์ •์ ์ž„ >> ๊ฐ€์žฅ ๋์— ์žˆ๋Š” ์ •์  ๊ตฌํ•˜๊ธฐ

2. ๊ฐ€์žฅ ๋์— ์žˆ๋Š” ์ •์ ์—์„œ bfs ํƒ์ƒ‰ > ๋‘ ์  ์‚ฌ์ด ์ค‘ ๊ฐ€์žฅ ๊ธด ๊ฑฐ๋ฆฌ ๊ตฌํ•˜๊ธฐ ๊ฐ€๋Šฅ!

import java.util.*;
import java.lang.*;
import java.io.*;

// The main method must be in a class named "Main".
class Main {
    static int V; // ์ •์  ๊ฐœ์ˆ˜
    static int[] dist; // ๊ฑฐ๋ฆฌ ์ €์žฅ
    static ArrayList<Node>[] adjList; // ์ธ์ ‘๋…ธ๋“œ ์ €์žฅ
    static boolean[] visited; // ๋ฐฉ๋ฌธ์—ฌ๋ถ€ ์ €์žฅ
    
    static class Node{
        int to;
        int w;

        public Node(int to, int w){
            this.to = to;
            this.w = w;
        }
    }
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        // ์ •์  ๊ฐœ์ˆ˜ ์ž…๋ ฅ๋ฐ›๊ธฐ
        V = Integer.parseInt(br.readLine());
        
        //์ดˆ๊ธฐ์„ค์ •
        dist = new int[V+1]; // ์ •์ ๋ฒˆํ˜ธ 1๋ฒˆ๋ถ€ํ„ฐ ์‹œ์ž‘
        Arrays.fill(dist, 0);
        visited = new boolean[V+1];
        adjList = new ArrayList[V+1];
        for(int i=1; i<=V; i++){
            adjList[i] = new ArrayList<>();
        }
        
        // ๊ฐ„์„  ์ •๋ณด ์ž…๋ ฅ๋ฐ›๊ธฐ
        for(int i=1; i<=V;i++){
            st = new StringTokenizer(br.readLine());
            int a = Integer.parseInt(st.nextToken());
            
            while(true){
                int b = Integer.parseInt(st.nextToken());
                if(b==-1){
                    break;
                }
                int c = Integer.parseInt(st.nextToken());
                adjList[a].add(new Node(b, c));
            }
        }
        
        //bfs ์ž„์˜์˜ ์ •์ ๋ถ€ํ„ฐ ํƒ์ƒ‰ > ๊ฐ€์žฅ ๋์— ์žˆ๋Š” ์ •์  ๊ตฌํ•˜๊ธฐ
        bfs(1);

        //๊ฐ€์žฅ ๋์— ์žˆ๋Š” ์ •์ ์—์„œ ๊ฑฐ๋ฆฌ ํƒ์ƒ‰ > ์ตœ๋Œ“๊ฐ’ ๊ตฌํ•˜๊ธฐ
        //dist ์ œ์ผ ํฐ ๊ฐ’์ธ ์ •์  ๊ตฌํ•˜๊ธฐ
        int max = 1;
        for(int i=2; i<=V;i++){
            //System.out.print(dist[i]+" ");
            if(dist[max] < dist[i]){
                max = i;
            }
        }
        
        //dist, visited ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”
        Arrays.fill(dist, 0);
        Arrays.fill(visited, false);
        bfs(max);

        //๊ฒฐ๊ณผ ์ถœ๋ ฅ > dist ์ •๋ ฌ ํ›„ ์ œ์ผ ๋งˆ์ง€๋ง‰(์ตœ๋Œ“๊ฐ’) ์ถœ๋ ฅ
        Arrays.sort(dist);
        System.out.println(dist[V]);
    } //main

    //bfs
    static void bfs(int start){
        ArrayDeque<Node> q = new ArrayDeque<>();
        dist[start] = 0;
        q.offer(new Node(start, 0));
        visited[start] = true;

        while(!q.isEmpty()){
            Node current = q.poll();

            for(Node next : adjList[current.to]){
                if(visited[next.to]){
                    continue;
                }
                visited[next.to] = true;
                q.offer(next);
                dist[next.to] = dist[current.to] + next.w;
            }
        } //while๋ฌธ
    }
}