#[allow(unused_imports)]
use std::io::{stdin, Read, StdinLock};
#[allow(unused_imports)]
use std::cmp::{max, min, Ordering};
#[allow(unused_imports)]
use std::str::FromStr;
#[allow(unused_imports)]
use std::collections::{HashSet, HashMap, BinaryHeap};
#[allow(unused_imports)]
use std::vec::Vec;

#[allow(dead_code)]
const INF: i32 = 1000_000_000;
#[allow(dead_code)]
const INFLL: i64 = 1000_000_000_000_000_000;
#[allow(dead_code)]
const EPS: f64 = 1.0e-10;
#[allow(dead_code)]
const MOD: i32 = 1000_000_007;

struct Scanner<'a> {
    cin: StdinLock<'a>,
}

impl<'a> Scanner<'a> {
    #[allow(dead_code)]
    fn new(cin: StdinLock<'a>) -> Scanner<'a> {
        Scanner {cin: cin}
    }

    #[allow(dead_code)]
    fn read1<T: FromStr>(&mut self) -> Option<T> {
        let token = self.cin.by_ref().bytes().map(|c| c.unwrap() as char)
                    .skip_while(|c| c.is_whitespace())
                    .take_while(|c| !c.is_whitespace())
                    .collect::<String>();
        token.parse::<T>().ok()
    }

    #[allow(dead_code)]
    fn read<T: FromStr>(&mut self) -> T {
        self.read1().unwrap()
    }
}

#[derive(Eq, PartialEq, PartialOrd, Clone)]
struct Edge {
    from: usize,
    to: usize,
    weight: i32
}

#[allow(dead_code)]
impl Edge {
    fn new(from: usize, to: usize, weight: i32) -> Edge {
        Edge { from: from, to: to, weight: weight }
    }
}

impl Ord for Edge {
    fn cmp(&self, other: &Edge) -> Ordering {
        if self.weight > other.weight {
            Ordering::Greater
        } else if self.weight < other.weight {
            Ordering::Less
        } else {
            Ordering::Equal
        }
    }
}

/// Adjacent List Graph
struct AdjListGraph {
    adj_list: std::vec::Vec<std::vec::Vec<Edge>>
}

#[allow(dead_code)]
impl AdjListGraph {
    fn new(n: usize) -> AdjListGraph {
        AdjListGraph {
            adj_list: vec![std::vec::Vec::<Edge>::new(); n],
        }
    }

    fn add_edge(&mut self, e: Edge) {
        self.adj_list[e.from].push(e);
    }

    fn edges(&self, v: usize) -> &std::vec::Vec<Edge> {
        &self.adj_list[v]
    }

    fn node_size(&self) -> usize {
        self.adj_list.len()
    }
}

#[allow(dead_code)]
fn dijkstra(g: &AdjListGraph, s: usize) -> std::vec::Vec<i32> {
    let mut d = vec![INF; g.node_size()];
    d[s] = 0;
    let mut que: BinaryHeap<(i32, usize)> = BinaryHeap::new();
    que.push((0, s));
    while !que.is_empty() {
        let (cur_d, v) = que.pop().unwrap();
        if d[v] < -cur_d {
            continue;
        }
        for ref e in g.edges(v) {
            if d[e.to] > -cur_d + e.weight {
                d[e.to] = -cur_d + e.weight;
                que.push((-d[e.to], e.to));
            }
        }
    }
    d
}

fn main() {
    let cin = stdin();
    let cin = cin.lock();
    let mut sc = Scanner::new(cin);

    let n: usize = sc.read();
    let m: usize = sc.read();
    let r: usize = sc.read();

    let mut g = AdjListGraph::new(n);
    for _i in 0..m {
        let s: usize = sc.read();
        let t: usize = sc.read();
        let w: i32 = sc.read();
        g.add_edge(Edge::new(s, t, w));
    }

    let dist = dijkstra(&g, r);
    for i in 0..n {
        match dist[i] {
            INF => println!("INF"),
            d   => println!("{}", d),
        }
    }
}


Measured memory_bytes: 27444