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 weight; 
        public Node(int value, int weight) {
            this.value = value;
            this.weight = weight;
        }
    }
    
    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>[] adj_List = new ArrayList[N+1];
        for (int i=1; i<=N; i++) {
            adj_List[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());
            adj_List[a].add(new Node(b,c));           
        }

        int[] dist = new int[N+1];
        Arrays.fill(dist, INF);
        dist[s] = 0; 
        int count = 0;
        PriorityQueue<Node> pq = new PriorityQueue<>(new Comparator<Node>(){
            @Override
            public int compare(Node n1, Node n2) {
                if (n1.weight != n2.weight){
                    return n1.weight-n2.weight;
                } 
                return n1.value - n2.value;
        }
        });
            
        pq.add(new Node(s,0)); 
        
        while (!pq.isEmpty()) {
            Node now_node = pq.poll();
            
            int now_v = now_node.value;
            int now_w = now_node.weight;

            
            if (dist[now_v] < now_w) continue;
            if (now_v != s){
                System.out.println(now_v);
               
            }

            for (Node next : adj_List[now_v]) {
                if (dist[next.value] > dist[now_v] + next.weight) {
                    dist[next.value] = dist[now_v] + next.weight;
                    pq.add(new Node(next.value, dist[next.value]));
                }
            }           
        }
        
        for (int i=1; i< N+1; i++) {
            if(dist[i] == INF) System.out.println("inf"); 
            else System.out.println(dist[i]);
        }

    }
}

Embed on website

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