#[doc = " https://github.com/hatoo/competitive-rust-snippets"]
#[allow(unused_imports)]
use std::cmp::{max, min, Ordering};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::io::{stdin, stdout, BufWriter, Write};
#[allow(unused_imports)]
use std::iter::FromIterator;
#[allow(unused_macros)]
macro_rules ! debug { ( $ ( $ a : expr ) ,* ) => { eprintln ! ( concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) ; } }
#[macro_export]
macro_rules ! input { ( source = $ s : expr , $ ( $ r : tt ) * ) => { let mut parser = Parser :: from_str ( $ s ) ; input_inner ! { parser , $ ( $ r ) * } } ; ( parser = $ parser : ident , $ ( $ r : tt ) * ) => { input_inner ! { $ parser , $ ( $ r ) * } } ; ( new_stdin_parser = $ parser : ident , $ ( $ r : tt ) * ) => { let stdin = std :: io :: stdin ( ) ; let reader = std :: io :: BufReader :: new ( stdin . lock ( ) ) ; let mut $ parser = Parser :: new ( reader ) ; input_inner ! { $ parser , $ ( $ r ) * } } ; ( $ ( $ r : tt ) * ) => { input ! { new_stdin_parser = parser , $ ( $ r ) * } } ; }
#[macro_export]
macro_rules ! input_inner { ( $ parser : ident ) => { } ; ( $ parser : ident , ) => { } ; ( $ parser : ident , $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let $ var = read_value ! ( $ parser , $ t ) ; input_inner ! { $ parser $ ( $ r ) * } } ; }
#[macro_export]
macro_rules ! read_value { ( $ parser : ident , ( $ ( $ t : tt ) ,* ) ) => { ( $ ( read_value ! ( $ parser , $ t ) ) ,* ) } ; ( $ parser : ident , [ $ t : tt ; $ len : expr ] ) => { ( 0 ..$ len ) . map ( | _ | read_value ! ( $ parser , $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ parser : ident , chars ) => { read_value ! ( $ parser , String ) . chars ( ) . collect ::< Vec < char >> ( ) } ; ( $ parser : ident , usize1 ) => { read_value ! ( $ parser , usize ) - 1 } ; ( $ parser : ident , $ t : ty ) => { $ parser . next ::<$ t > ( ) . expect ( "Parse error" ) } ; }
use std::io;
use std::io::BufRead;
use std::str;
pub struct Parser<R> {
    reader: R,
    buf: Vec<u8>,
    pos: usize,
}
impl Parser<io::Empty> {
    pub fn from_str(s: &str) -> Parser<io::Empty> {
        Parser {
            reader: io::empty(),
            buf: s.as_bytes().to_vec(),
            pos: 0,
        }
    }
}
impl<R: BufRead> Parser<R> {
    pub fn new(reader: R) -> Parser<R> {
        Parser {
            reader: reader,
            buf: vec![],
            pos: 0,
        }
    }
    pub fn update_buf(&mut self) {
        self.buf.clear();
        self.pos = 0;
        loop {
            let (len, complete) = {
                let buf2 = self.reader.fill_buf().unwrap();
                self.buf.extend_from_slice(buf2);
                let len = buf2.len();
                if len == 0 {
                    break;
                }
                (len, buf2[len - 1] <= 0x20)
            };
            self.reader.consume(len);
            if complete {
                break;
            }
        }
    }
    pub fn next<T: str::FromStr>(&mut self) -> Result<T, T::Err> {
        loop {
            let mut begin = self.pos;
            while begin < self.buf.len() && (self.buf[begin] <= 0x20) {
                begin += 1;
            }
            let mut end = begin;
            while end < self.buf.len() && (self.buf[end] > 0x20) {
                end += 1;
            }
            if begin != self.buf.len() {
                self.pos = end;
                return str::from_utf8(&self.buf[begin..end]).unwrap().parse::<T>();
            } else {
                self.update_buf();
            }
        }
    }
}
#[allow(unused_macros)]
macro_rules ! debug { ( $ ( $ a : expr ) ,* ) => { eprintln ! ( concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) ; } }
#[doc = " https://github.com/hatoo/competitive-rust-snippets"]
const BIG_STACK_SIZE: bool = true;
#[allow(dead_code)]
fn main() {
    use std::thread;
    if BIG_STACK_SIZE {
        thread::Builder::new()
            .stack_size(32 * 1024 * 1024)
            .name("solve".into())
            .spawn(solve)
            .unwrap()
            .join()
            .unwrap();
    } else {
        solve();
    }
}

pub struct SCC<'a> {
    g: &'a [Vec<usize>],
    r_g: Vec<Vec<usize>>,
    post_order: VecDeque<usize>,
    used: Vec<bool>,
    pub order: Vec<usize>,
}

impl <'a> SCC<'a> {
    pub fn new(g: &'a [Vec<usize>]) -> Self {
        let n = g.len();
        let mut r_g = vec![vec![]; n];
        for u in 0..n {
            let conn = &g[u];
            for &v in conn {
                r_g[v].push(u);
            }
        }
        Self {
            g,
            r_g,
            post_order: VecDeque::new(),
            used: vec![false; n],
            order: vec![n; n],
        }
    }
    fn dfs(&mut self, u: usize) {
        self.used[u] = true;
        for i in 0 .. self.g[u].len() {
            let v = self.g[u][i];
            if !self.used[v] {
                self.dfs(v);
            }
        }
        self.post_order.push_front(u);
    }
    fn rdfs(&mut self, u: usize, k: usize) {
        self.used[u] = true;
        self.order[u] = k;
        for i in 0 .. self.r_g[u].len() {
            let v = self.r_g[u][i];
            if !self.used[v] {
                self.rdfs(v, k);
            }
        }
    }
    pub fn build(&mut self) {
        for v in 0 .. self.g.len() {
            if !self.used[v] {
                self.dfs(v);
            }
        }
        self.used = vec![false; self.g.len()];
        let mut k = 0;
        for i in 0 .. self.post_order.len() {
            let v = self.post_order[i];
            if !self.used[v] {
                self.rdfs(v, k);
                k += 1;
            }
        }
    }
}

#[derive(Debug, Clone, Copy)]
struct Edge(usize, u64);
fn min_edge(edges: &[Edge]) -> &Edge {
    let mut r = &edges[0];
    for e in edges {
        if e.1 < r.1 {
            r = e;
        }
    }
    r
}
fn chu_liu_edmonds(in_g: &[Vec<Edge>], root: usize) -> u64 {
    let mut min_in_g: Vec<Edge> = vec![];
    let mut min_out_g: Vec<Vec<usize>> = vec![vec![]; in_g.len()];
    for to in 0..in_g.len() {
        if to == root {
            min_in_g.push(Edge(1<<40, 0)); // null value
            continue;
        }
        let e = min_edge(&in_g[to]);
        min_in_g.push(e.clone());
        min_out_g[e.0].push(to);
    }

    let mut scc = SCC::new(&min_out_g);
    scc.build();

    let mut max_cmp = 0;
    for &cmp in &scc.order {
        if cmp > max_cmp {
            max_cmp = cmp;
        }
    }

    let no_loop = max_cmp == scc.order.len()-1;
    if no_loop {
        let mut res = 0;
        for e in &min_in_g {
            res += e.1;
        }
        return res;
    }

    let mut groups = vec![vec![]; max_cmp+1];
    for v in 0..scc.order.len() {
        let cmp = scc.order[v];
        groups[cmp].push(v);
    }

    let mut contracted_cost = 0;
    let mut new_in_g = vec![vec![]; max_cmp+1];
    for group in groups {
        if group.len() > 1 { // loop
            let cmp_to = scc.order[group[0]];
            for &v in &group {
                let cur_e = min_in_g[v].clone();

                contracted_cost += cur_e.1;

                for e in &in_g[v] {
                    let in_group = group.contains(&e.0);
                    if !in_group {
                        let cmp_from = scc.order[e.0];
                        let diff_cost = e.1 - cur_e.1;
                        new_in_g[cmp_to].push(Edge(cmp_from, diff_cost));
                    }
                }
            }
        } else {
            assert!(group.len() == 1);
            let v = group[0];
            for e in &in_g[v] {
                let cmp_to = scc.order[v];
                let cmp_from = scc.order[e.0];
                new_in_g[cmp_to].push(Edge(cmp_from, e.1));
            }
        }
    }

    let new_root = scc.order[root];

    contracted_cost + chu_liu_edmonds(&new_in_g, new_root)
}
fn solve() {
    input! {
        V: usize, E: usize, r: usize,
        ES: [(usize, usize, u64); E],
    };

    let mut g = vec![vec![]; V];
    for i in 0..E {
        let e = ES[i];
        g[e.1].push(Edge(e.0,e.2));
    }

    println!("{}", chu_liu_edmonds(&g, r));
}


Measured memory_bytes: 9096