output_description: Print the sum of the weights the Minimum-Cost Arborescence.
input_description: |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.
attempt: import heapq as pq
  n,E,r = map(int, input().split())
  M = [[] for i in range(n)]
  
  for i in range(E):
   s,t,w = map(int, input().split())
   M[s].append([t,w])
  
  def prim(r):
   color = [0] * n
   d = [float("inf")] * n
   d[r] = 0
   H = []
   pq.heappush(H, [0, r])
   while H:
    u2,u1 = pq.heappop(H)
    color[u1] = 1
    if d[u1] < u2: continue
    for v1, v2 in M[u1]:
     if color[v1] == 1: continue
     if d[v1] > v2:
      d[v1] = v2
      pq.heappush(H, [d[v1], v1])
   return sum(d)
   
  print(prim(r))
problem_description: 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).
samples: [{"input": "4 6 0\n    0 1 3\n    0 2 2\n    2 0 1\n    2 3 1\n    3 0 1\n    3 1 5", "output": "6"}, {"input": "6 10 0\n    0 2 7\n    0 1 1\n    0 3 5\n    1 4 9\n    2 1 6\n    1 3 2\n    3 4 3\n    4 2 2\n    2 5 8\n    3 5 3", "output": "11"}]