Language: C++

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;

using static util;
using P = pair<int, int>;

class Program
{
    static StreamWriter sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
    static Scan sc = new Scan();
    const int M = 1000000007;
    // const int M = 998244353;
    static void Main()
    {
        int n, m, s;
        sc.Multi(out n, out m, out s);
        // --s;
        var edges = new List<pair<long, int>>[n];
        for (int i = 0; i < n; i++)
        {
            edges[i] = new List<pair<long, int>>();
        }
        for (int i = 0; i < m; i++)
        {
            int f, t, c;
            sc.Multi(out f, out t, out c);
            // --f;
            // --t;
            edges[f].Add(make_pair((long)c, t));
        }
        sw.WriteLine(MSA.getmincost(edges, s));
        sw.Flush();
    }
}
class pair<T, U> : IComparable<pair<T, U>> where T : IComparable<T> where U : IComparable<U>
{
    public T v1;
    public U v2;
    public pair(T v1, U v2) {
        this.v1 = v1;
        this.v2 = v2;
    }
    public int CompareTo(pair<T, U> a) { return v1.CompareTo(a.v1) != 0 ? v1.CompareTo(a.v1) : v2.CompareTo(a.v2); }
    public override string ToString() { return v1 + " " + v2; }
}
static class util
{
    public static pair<T, U> make_pair<T, U>(T v1, U v2) where T : IComparable<T> where U : IComparable<U> { return new pair<T, U>(v1, v2); }
    public static void swap<T>(ref T a, ref T b) { var t = a; a = b; b = t; }
    public static void swap<T>(this IList<T> a, int i, int j) { var t = a[i]; a[i] = a[j]; a[j] = t; }
    public static T[] copy<T>(this IList<T> a) {
        var ret = new T[a.Count];
        for (int i = 0; i < a.Count; i++) ret[i] = a[i];
        return ret;
    }
}

class Scan
{
    public string Str { get { return Console.ReadLine().Trim(); } }
    public string[] StrArr { get { return Str.Split(new []{' '}, System.StringSplitOptions.RemoveEmptyEntries); } }
    bool eq<T, U>() { return typeof(T).Equals(typeof(U)); }
    T ct<T, U>(U a) { return (T)Convert.ChangeType(a, typeof(T)); }
    T cv<T>(string s) { return eq<T, int>()    ? ct<T, int>(int.Parse(s))
                       : eq<T, long>()   ? ct<T, long>(long.Parse(s))
                       : eq<T, double>() ? ct<T, double>(double.Parse(s))
                       : eq<T, char>()   ? ct<T, char>(s[0])
                                         : ct<T, string>(s); }
    public void Multi<T, U>(out T a, out U b)
    { var ar = StrArr; a = cv<T>(ar[0]); b = cv<U>(ar[1]); }
    public void Multi<T, U, V>(out T a, out U b, out V c)
    { var ar = StrArr; a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); }
    public void Multi<T, U, V, W>(out T a, out U b, out V c, out W d)
    { var ar = StrArr; a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); }
    public void Multi<T, U, V, W, X>(out T a, out U b, out V c, out W d, out X e)
    { var ar = StrArr; a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); e = cv<X>(ar[4]); }
    public void Multi<T, U, V, W, X, Y>(out T a, out U b, out V c, out W d, out X e, out Y f)
    { var ar = StrArr; a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); e = cv<X>(ar[4]); f = cv<Y>(ar[5]); }
}

// 最小有向全域木
static class MSA
{
    class node {
        PriorityQueue<pair<long, int>> come_edges;
        long additional_cost;

        // init
        public node() {
            come_edges = new PriorityQueue<pair<long, int>>(){ rev = true };
            additional_cost = 0;
        }
        public void push_edge(pair<long, int> e) {
            e.v1 -= additional_cost;
            come_edges.Push(e);
        }
        public pair<long, int> pop_edge() {
            var ret = come_edges.Pop();
            ret.v1 += additional_cost;
            return ret;
        }
        public long add_cost(long ad) { return additional_cost += ad; }
        public int edge_cnt { get { return come_edges.Count; } }

        // hakai-teki
        public static node merge(node a, node b) {
            if (a.edge_cnt < b.edge_cnt) return merge(b, a);
            while (b.edge_cnt > 0) {
                var e = b.pop_edge();
                a.push_edge(e);
            }
            return a;
        }
    }
    public static long getmincost(List<pair<long, int>>[] edges, int root) {
        int n = edges.Length;
        var nodes = new node[n];
        for (int i = 0; i < n; i++)
        {
            nodes[i] = new node();
        }
        for (int i = 0; i < n; i++)
        {
            foreach (var item in edges[i])
                nodes[item.v2].push_edge(make_pair(item.v1, i));
        }
        var leaf_node = new int[n];
        for (int i = 0; i < n; i++)
        {
            leaf_node[i] = -1;
        }
        leaf_node[root] = root;
        var uf_zipped = new UnionFind(n);
        var uf_connected = new UnionFind(n);
        long ret = 0;
        for (int i = 0; i < n; i++)
        {
            var st = new Stack<pair<long, int>>();
            for (int j = uf_zipped.getpar(i); leaf_node[j] == -1; )
            {
                leaf_node[j] = i;
                if (nodes[j].edge_cnt == 0) return -1;
                var e = nodes[j].pop_edge();
                e.v2 = uf_zipped.getpar(e.v2);
                if (j == e.v2) continue;
                st.Push(e);
                ret += e.v1;
                nodes[j].add_cost(-e.v1);
                if (uf_connected.same(j, e.v2)) {
                    uf_zipped.unite(j, e.v2);
                    nodes[uf_zipped.getpar(j)] = node.merge(nodes[j], nodes[e.v2]);
                    j = uf_zipped.getpar(j);
                    while (st.Any()) {
                        var ee = st.Peek();
                        if (uf_zipped.getpar(e.v2) == j) break;

                        st.Pop();
                        uf_zipped.unite(j, ee.v2);
                        nodes[uf_zipped.getpar(j)] = node.merge(nodes[j], nodes[e.v2]);
                        j = uf_zipped.getpar(j);
                    }
                    leaf_node[j] = -1;
                }
                else {
                    uf_connected.unite(j, e.v2);
                    j = e.v2;
                }
            }
        }
        return ret;
    }
}
class UnionFind
{
    int num;
    int[] par, sz, rank;
    public UnionFind(int n) {
        num = n; par = new int[n]; sz = new int[n]; rank = new int[n];
        for (int i = 0; i < n; ++i) { par[i] = i; sz[i] = 1; rank[i] = 0; }
    }
    int find(int x) => par[x] == x ? x : (par[x] = find(par[x]));
    public bool same(int x, int y) { return find(x) == find(y); }
    public int unionsize(int x) { return sz[find(x)]; }
    public bool unite(int x, int y) {
        x = find(x); y = find(y);
        if (x == y) return false;
        --num;
        if (rank[x] < rank[y]) {
            par[x] = y;
            sz[y] += sz[x];
        }
        else {
            par[y] = x;
            sz[x] += sz[y];
            if (rank[x] == rank[y]) ++rank[x];
        }
        return true;
    }
    public bool ispar(int x) { return x == find(x); }
    public int getpar(int x) { return find(x); }
}

// the greatest element pops
class PriorityQueue<T> where T : IComparable<T> {
    List<T> buf;
    public bool rev = false;
    public PriorityQueue() { buf = new List<T>(); }
    int cmp(int i, int j) { return buf[i].CompareTo(buf[j]) * (rev ? -1 : 1); }
    void swap(int i, int j) { var t = buf[i]; buf[i] = buf[j]; buf[j] = t; }
    public void Push(T elem) {
        int n = buf.Count;
        buf.Add(elem);
        while (n > 0) {
            int i = (n - 1) >> 1;
            if (cmp(n, i) > 0) swap(i, n);
            n = i;
        }
    }
    public T Pop() {
        T ret = buf[0];
        int n = buf.Count - 1;
        buf[0] = buf[n];
        buf.RemoveAt(n);
        for (int i = 0, j; (j = (i << 1) + 1) < n; i = j) {
            if (j != n - 1 && cmp(j, j + 1) < 0) ++j;
            if (cmp(i, j) < 0) swap(i, j);
        }
        return ret;
    }
    public T Top { get { return buf[0]; } }
    public int Count { get { return buf.Count; } }

    // hakai-teki
    static PriorityQueue<T> merge(PriorityQueue<T> q1, PriorityQueue<T> q2) {
        if (q1.Count < q2.Count) return merge(q2, q1);
        while (q2.Count > 0) q1.Push(q2.Pop());
        return q1;
    }
}



Compiler output:
a.cc:1:7: error: expected nested-name-specifier before 'System'
    1 | using System;
      |       ^~~~~~
a.cc:2:7: error: expected nested-name-specifier before 'System'
    2 | using System.Collections.Generic;
      |       ^~~~~~
a.cc:3:7: error: expected nested-name-specifier before 'System'
    3 | using System.Linq;
      |       ^~~~~~
a.cc:4:7: error: expected nested-name-specifier before 'System'
    4 | using System.IO;
      |       ^~~~~~
a.cc:6:7: error: expected nested-name-specifier before 'static'
    6 | using static util;
      |       ^~~~~~
a.cc:7:11: error: 'pair' does not name a type
    7 | using P = pair<int, int>;
      |           ^~~~
a.cc:11:12: error: 'StreamWriter' does not name a type
   11 |     static StreamWriter sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
      |            ^~~~~~~~~~~~
a.cc:12:12: error: 'Scan' does not name a type
   12 |     static Scan sc = new Scan();
      |            ^~~~
a.cc:36:2: error: expected ';' after class definition
   36 | }
      |  ^
      |  ;
a.cc: In static member function 'static void Program::Main()':
a.cc:18:9: error: 'sc' was not declared in this scope; did you mean 's'?
   18 |         sc.Multi(out n, out m, out s);
      |         ^~
      |         s
a.cc:18:18: error: 'out' was not declared in this scope
   18 |         sc.Multi(out n, out m, out s);
      |                  ^~~
a.cc:18:29: error: expected ')' before 'm'
   18 |         sc.Multi(out n, out m, out s);
      |                             ^
a.cc:18:17: note: to match this '('
   18 |         sc.Multi(out n, out m, out s);
      |                 ^
a.cc:18:36: error: expected ')' before 's'
   18 |         sc.Multi(out n, out m, out s);
      |                                    ^
a.cc:18:17: note: to match this '('
   18 |         sc.Multi(out n, out m, out s);
      |                 ^
a.cc:20:9: error: 'var' was not declared in this scope
   20 |         var edges = new List<pair<long, int>>[n];
      |         ^~~
a.cc:23:13: error: 'edges' was not declared in this scope
   23 |             edges[i] = new List<pair<long, int>>();
      |             ^~~~~
a.cc:23:28: error: 'List' does not name a type
   23 |             edges[i] = new List<pair<long, int>>();
      |                            ^~~~
a.cc:23:33: error: 'pair' was not declared in this scope
   23 |             edges[i] = new List<pair<long, int>>();
      |                                 ^~~~
a.cc:23:38: error: expected primary-expression before 'long'
   23 |             edges[i] = new List<pair<long, int>>();
      |                                      ^~~~
a.cc:28:26: error: expected ')' before 'f'
   28 |             sc.Multi(out f, out t, out c);
      |                          ^
a.cc:28:21: note: to match this '('
   28 |             sc.Multi(out f, out t, out c);
      |                     ^
a.cc:28:33: error: expected ')' before 't'
   28 |             sc.Multi(out f, out t, out c);
      |                                 ^
a.cc:28:21: note: to match this '('
   28 |             sc.Multi(out f, out t, out c);
      |                     ^
a.cc:28:40: error: expected ')' before 'c'
   28 |             sc.Multi(out f, out t, out c);
      |                                        ^
a.cc:28:21: note: to match this '('
   28 |             sc.Multi(out f, out t, out c);
      |                     ^
a.cc:31:13: error: 'edges' was not declared in this scope
   31 |             edges[f].Add(make_pair((long)c, t));
      |             ^~~~~
a.cc:31:26: error: 'make_pair' was not declared in this scope
   31 |             edges[f].Add(make_pair((long)c, t));
      |                          ^~~~~~~~~
a.cc:33:9: error: 'sw' was not declared in this scope; did you mean 's'?
   33 |         sw.WriteLine(MSA.getmincost(edges, s));
      |         ^~
      |         s
a.cc:33:22: error: 'MSA' was not declared in this scope
   33 |         sw.WriteLine(MSA.getmincost(edges, s));
      |                      ^~~
a.cc:33:37: error: 'edges' was not declared in this scope
   33 |         sw.WriteLine(MSA.getmincost(edges, s));
      |                                     ^~~~~
a.cc: At global scope:
a.cc:37:7: error: 'pair' is not a class template
   37 | class pair<T, U> : IComparable<pair<T, U>> where T : IComparable<T> where U : IComparable<U>
      |       ^~~~
a.cc:37:12: error: 'T' was not declared in this scope
   37 | class pair<T, U> : IComparable<pair<T, U>> where T : IComparable<T> where U : IComparable<U>
      |            ^
a.cc:37:15: error: 'U' was not declared in this scope
   37 | class pair<T, U> : IComparable<pair<T, U>> where T : IComparable<T> where U : IComparable<U>
      |               ^
a.cc:37:31: error: expected template-name before '<' token
   37 | class pair<T, U> : IComparable<pair<T, U>> where T : IComparable<T> where U : IComparable<U>
      |                               ^
a.cc:37:31: error: expected '{' before '<' token
a.cc:37:31: error: expected unqualified-id before '<' token
a.cc:50:11: error: expected ':' before 'static'
   50 |     public static pair<T, U> make_pair<T, U>(T v1, U v2) where T : IComparable<T> where U : IComparable<U> { return new pair<T, U>(v1, v2); }
      |           ^~~~~~~
      |           :
a.cc:50:19: error: 'pair' is not a template
   50 |     public static pair<T, U> make_pair<T, U>(T v1, U v2) where T : IComparable<T> where U : IComparable<U> { return new pair<T, U>(v1, v2); }
      |                   ^~~~
a.cc:50:24: error: 'T' was not declared in this scope
   50 |     public static pair<T, U> make_pair<T, U>(T v1, U v2) where T : IComparable<T> where U : IComparable<U> { return new pair<T, U>(v1, v2); }
      |                        ^
a.cc:50:27: error: 'U' was not declared in this scope
   50 |     public static pair<T, U> make_pair<T, U>(T v1, U v2) where T : IComparable<T> where U : IComparable<U> { return new pair<T, U>(v1, v2); }
      |                           ^
a.cc:50:30: error: expected ';' at end of member declaration
   50 |     public static pair<T, U> make_pair<T, U>(T v1, U v2) where T : IComparable<T> where U : IComparable<U> { return new pair<T, U>(v1, v2); }
      |                              ^~~~~~~~~
      |                                       ;
a.cc:50:39: error: expected unqualified-id before '<' token
   50 |     public static pair<T, U> make_pair<T, U>(T v1, U v2) where T : IComparable<T> where U : IComparable<U> { return new pair<T, U>(v1, v2); }
      |                                       ^
a.cc:51:11: error: expected ':' before 'static'
   51 |     public static void swap<T>(ref T a, ref T b) { var t = a; a = b; b = t; }
      |           ^~~~~~~
      |           :
a.cc:51:24: error: variable or field 'swap' declared void
   51 |     public static void swap<T>(ref T a, ref T b) { var t = a; a = b; b = t; }
      |                        ^~~~
a.cc:51:24: error: expected ';' at end of member declaration
   51 |     public static void swap<T>(ref T a, ref T b) { var t = a; a = b; b = t; }
      |                        ^~~~
      |                            ;
a.cc:51:28: error: expected unqualified-id before '<' token
   51 |     public static void swap<T>(ref T a, ref T b) { var t = a; a = b; b = t; }
      |                            ^
a.cc:52:11: error: expected ':' before 'static'
   52 |     public static void swap<T>(this IList<T> a, int i, int j) { var t = a[i]; a[i] = a[j]; a[j] = t; }
      |           ^~~~~~~
      |           :
a.cc:52:24: error: variable or field 'swap' declared void
   52 |     public static void swap<T>(this IList<T> a, int i, int j) { var t = a[i]; a[i] = a[j]; a[j] = t; }
      |                        ^~~~
a.cc:52:24: error: expected ';' at end of member declaration
   52 |     public static void swap<T>(this IList<T> a, int i, int j) { var t = a[i]; a[i] = a[j]; a[j] = t; }
      |                        ^~~~
      |                            ;
a.cc:52:28: error: expected unqualified-id before '<' token
   52 |     public static void swap<T>(this IList<T> a, int i, int j) { var t = a[i]; a[i] = a[j]; a[j] = t; }
      |                            ^
a.cc:53:11: error: expected ':' before 'static'
   53 |     public static T[] copy<T>(this IList<T> a) {
      |           ^~~~~~~
      |           :
a.cc:53:19: error: 'T' does not name a type
   53 |     public static T[] copy<T>(this IList<T> a) {
      |                   ^
a.cc:58:2: error: expected ';' after class definition
   58 | }
      |  ^
      |  ;
a.cc:48:1: error: a storage class can only be specified for objects and functions
   48 | static class util
      | ^~~~~~
a.cc:62:11: error: expected ':' before 'string'
   62 |     public string Str { get { return Console.ReadLine().Trim(); } }
      |           ^~~~~~~
      |           :
a.cc:62:12: error: 'string' does not name a type
   62 |     public string Str { get { return Console.ReadLine().Trim(); } }
      |            ^~~~~~
a.cc:63:11: error: expected ':' before 'string'
   63 |     public string[] StrArr { get { return Str.Split(new []{' '}, System.StringSplitOptions.RemoveEmptyEntries); } }
      |           ^~~~~~~
      |           :
a.cc:63:12: error: 'string' does not name a type
   63 |     public string[] StrArr { get { return Str.Split(new []{' '}, System.StringSplitOptions.RemoveEmptyEntries); } }
      |            ^~~~~~
a.cc:64:10: error: expected ';' at end of member declaration
   64 |     bool eq<T, U>() { return typeof(T).Equals(typeof(U)); }
      |          ^~
      |            ;
a.cc:64:12: error: expected unqualified-id before '<' token
   64 |     bool eq<T, U>() { return typeof(T).Equals(typeof(U)); }
      |            ^
a.cc:65:5: error: 'T' does not name a type
   65 |     T ct<T, U>(U a) { return (T)Convert.ChangeType(a, typeof(T)); }
      |     ^
a.cc:66:5: error: 'T' does not name a type
   66 |     T cv<T>(string s) { return eq<T, int>()    ? ct<T, int>(int.Parse(s))
      |     ^
a.cc:71:11: error: expected ':' before 'void'
   71 |     public void Multi<T, U>(out T a, out U b)
      |           ^~~~~
      |           :
a.cc:71:17: error: variable or field 'Multi' declared void
   71 |     public void Multi<T, U>(ou