Problem:
Minimum-Cost Arborescence

Find the sum of the weights of edges of the Minimum-Cost Arborescence with the root r for a given weighted directed graph G = (V, E).

Input

|V| |E| r
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1

, where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively. r is the root of the Minimum-Cost Arborescence.

si and ti represent source and target verticess of i-th directed edge. wi represents the weight of the i-th directed edge.

Output

Print the sum of the weights the Minimum-Cost Arborescence.

Constraints

1 ≤ |V| ≤ 100

0 ≤ |E| ≤ 1,000

0 ≤ wi ≤ 10,000

G has arborescence(s) with the root r

Sample Input 1

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

Sample Output 1

6

Sample Input 2

6 10 0
0 2 7
0 1 1
0 3 5
1 4 9
2 1 6
1 3 2
3 4 3
4 2 2
2 5 8
3 5 3

Sample Output 2

11

Solution in C# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
static class Program{
	const int mod=(int)1e9+7;
	static void Main(){
		Sc sc=new Sc();
		int[] s=sc.Ia;
		int n=s[0],m=s[1],o=n;
		var li=new List<Tuple<int,int>>[n*2];
		int[] h=new int[n*2],p=new int[n*2],r=new int[n*2],g=new int[n*2];
		for(int i=0;i<n;i++){
			li[i]=new List<Tuple<int,int>>();
			g[i]=i;
			h[i]=-1;
		}
		for(int i=0;i<m;i++){
			int[] e=sc.Ia;
			li[e[1]].Add(Tuple.Create(e[0],e[2]));
		}
		long ans=0;
		for(int i = 0;i<=o;i++) {
			if(g[i]!=i){continue;}
			int v=i;
			while(h[v]==-1&&v!=s[2]){
				int u2=-1,uj=0;
				for(int j=1;j<li[v].Count;j++){
					if(li[v][uj].Item2>li[v][j].Item2){uj=j;}
				}
				u2=li[v][uj].Item1;
				while(u2!=g[u2]){u2=g[u2];}
				g[li[v][uj].Item1]=u2;
				p[v]=u2;
				r[v]=li[v][uj].Item2;
				h[v]=i;
				ans+=li[v][uj].Item2;
				v=u2;
				if(i==h[v]){
					li[o]=new List<Tuple<int,int>>();
					g[o]=o;
					h[o]=-1;
					int u=p[v];
					while(u!=g[u]){u=g[u];}
					g[p[v]]=u;
					while(g[u]!=o){
						g[u]=o;
						for(int j=0;j<li[u].Count;j++){
							if(li[u][j].Item1!=p[u]){
								int x=li[u][j].Item1;
								while(x!=g[x]){x=g[x];}
								g[li[u][j].Item1]=x;
								if(x!=o){li[o].Add(Tuple.Create(x,li[u][j].Item2-r[u]));}
							}
						}
						u=p[u];
						int q=u;
						while(u!=g[u]){u=g[u];}
						g[q]=u;
					}
					li[o]=li[o].GroupBy(z=>z.Item1).Select(x=>x.Min()).ToList();
					v=o;
					o++;
				}
			}
		}
		Console.WriteLine("{0}",ans);
	}
}
public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),int.Parse);}
	public int[] Ia3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),long.Parse);}
	public long[] La3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),long.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}