Problem:
Single Source Shortest Path

For a given weighted graph G(V, E) and a source r, find the source shortest path to each vertex from the source (SSSP: Single Source Shortest Path).

Input

An edge-weighted graph G (V, E) and the source r.

|V| |E| r
s0 t0 d0
s1 t1 d1
:
s|E|-1 t|E|-1 d|E|-1

|V| is the number of vertices and |E| is the number of edges in G. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively. r is the source of the graph.

si and ti represent source and target vertices of i-th edge (directed) and di represents the cost of the i-th edge.

Output

Print the costs of SSSP in the following format.

c0
c1
:
c|V|-1

The output consists of |V| lines. Print the cost of the shortest path from the source r to each vertex 0, 1, ... |V|-1 in order. If there is no path from the source to a vertex, print INF.

Constraints

1 ≤ |V| ≤ 100000

0 ≤ di ≤ 10000

0 ≤ |E| ≤ 500000

There are no parallel edges

There are no self-loops

Sample Input 1

4 5 0
0 1 1
0 2 4
1 2 2
2 3 1
1 3 5

Sample Output 1

0
1
3
4

Sample Input 2

4 6 1
0 1 1
0 2 4
2 0 1
1 2 2
3 1 1
3 2 5

Sample Output 2

3
0
2
INF

Solution in Rust (judge verdict: Accepted)

#[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),
        }
    }
}