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

// The main method must be in a class named "Main".
class Main {
    static class Node{
        int value; 
        int dist;
        Node(int value, int dist) {
            this.value = value;
            this.dist = dist;
        }
    }
    
    static ArrayList<Node> biHeap = new ArrayList<>();

    static boolean isSmaller(int a, int b) {
        Node nodeA = biHeap.get(a);
        Node nodeB = biHeap.get(b); 
        if (nodeA.dist < nodeB.dist) return true; 
        if (nodeA.dist == nodeB.dist && nodeA.value < nodeB.value) return true; 
        return false;
    }

    static void swap(int idx1, int idx2) {
        Node temp = biHeap.get(idx1);
        biHeap.set(idx1, biHeap.get(idx2));
        biHeap.set(idx2, temp);
    }

    static void heapPush(int vertex, int dist) {
        biHeap.add(new Node(vertex, dist));
        int current = biHeap.size()-1; 
        while (current>0) {
            int parent = (current-1)/2; 
            if (isSmaller(current, parent)) {
                swap(current, parent);
                //이거..리턴 제대로 안해서 문제 있을 듯? 
                // Collections.swap 쓸까 했는데 직접 다 구현하고 싶었음
                current = parent; 
            } else {
                break; 
            }
        }
    }

    static Node heapPop() {
        Node root = biHeap.get(0); 
        Node lastNode = biHeap.remove(biHeap.size()-1); 
        if (!biHeap.isEmpty()) {
            biHeap.set(0, lastNode); 
            int limit = biHeap.size();
            int current = 0;
            while(current < limit) {
                int left = current*2+1;
                int right = current*2 + 2; 
                int smallest_idx = current;
                if (left < limit && isSmaller(left, smallest_idx)) {
                    smallest_idx = left;
                }
                if (right < limit && isSmaller(right, smallest_idx)) {
                    smallest_idx = right;
                }
                if (smallest_idx == current) break;
                else {
                    swap(current, smallest_idx); 
                }
                current = smallest_idx;
            }
        }
        return root;            
    }
    

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        int s = Integer.parseInt(st.nextToken());
        int INF = Integer.MAX_VALUE;

        ArrayList<Node>[] adjList = new ArrayList[N+1];        
        
        for (int i=1; i<N+1; i++) {
            adjList[i] = new ArrayList<>();
        }
        
        for (int i=0; i<M; i++) {
            st = new StringTokenizer(br.readLine());
            int a = Integer.parseInt(st.nextToken());
            int b = Integer.parseInt(st.nextToken());
            int c = Integer.parseInt(st.nextToken());
            adjList[a].add(new Node(b,c));         
        }


        
        int[] dist_from = new int [N+1]; 
        Arrays.fill(dist_from, INF);

        

        dist_from[s] = 0;
        heapPush(s, 0);
        int count = 0;
        while (!biHeap.isEmpty()) {
            Node current = heapPop();

            if (dist_from[current.value] < current.dist) continue; 

            if (current.value != s) {
                count ++;
                System.out.println(current.value);
            }
            
            if (count == 2) break;

            for (Node neighbor : adjList[current.value]) {
                if (dist_from[neighbor.value] > current.dist + neighbor.dist) {
                    dist_from[neighbor.value] = current.dist + neighbor.dist;
                    heapPush(neighbor.value, dist_from[neighbor.value]);
                }
            }
        }

        while (count < 2) {
            System.out.println("inf");
            count ++;
        }


        
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: