, 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.
To solve this problem, we need to find the sum of the weights of the edges of the minimum-cost arborescence rooted at a given vertex ( r ) in a directed graph. An arborescence is a directed tree where every node except the root has exactly one parent, and there's exactly one path from the root to any other node.
Approach
The problem can be efficiently solved using Edmonds' algorithm for finding the minimum spanning arborescence in a directed graph. The key steps are as follows:
Identify Minimum Incoming Edges: For each node (except the root), find the minimum weight incoming edge.
Check for Cycles: If the selected edges form a cycle (excluding the root), we need to contract the cycle into a single node.
Recursive Contraction: Recursively apply the algorithm on the contracted graph until no cycles exist, then combine the results to form the minimum arborescence.
The steps involve using depth-first search (DFS) to detect cycles, and union-find techniques to manage node contractions.
Solution Code
python
import sys
sys.setrecursionlimit(1000000)
def main():
from sys import stdin
n, m, root = map(int, stdin.readline().split())
edges = []
incoming = [[] for _ in range(n)]
for _ in range(m):
s, t, w = map(int, stdin.readline().split())
edges.append((s, t, w))
incoming[t].append((s, t, w))
def edmonds(incoming_edges, root):
parent = [-1] * n
cost_total = 0
min_edges = [None] * n
for v in range(n):
if v == root:
continue
min_edge = None
min_weight = float('inf')
for e in incoming_edges[v]:
s, t, w = e
if w < min_weight:
min_edge = (s, t, w)
min_weight = w
if min_edge is None:
return -1 # This shouldn't happen per problem constraints
s, t, w = min_edge # can't happen because problem says there is an arborescence
min_edges[v] = min_edge
cost_total += w
parent[v] = s
visited = [False] * n
in_stack = [False] * n
path = []
cycle_found = False
def dfs(u):
nonlocal path, cycle_found
if cycle_found:
return
visited[u] = True
in_stack[u] = True
pv = parent[u]
if pv == -1:
in_stack[u] = False
return
if in_stack[pv]:
# Found a cycle starting at pv
path.append(pv)
current = u
while current != pv:
path.append(current)
current = parent[current]
path.append(pv) # Ensure the last is pv
cycle_found = True
return
if not visited[pv]:
dfs(pv)
in_stack[u] = False
for v in range(n):
if v == root:
continue
if not visited[v]:
path = []
dfs(v)
if cycle_found:
break
if not cycle_found:
return cost_total
# Now, contract the cycle
cycle = path # Extract the cycle nodes
new_node = n # new node number
new_count = n + 1
new_incoming = [[] for _ in range(new_count)]
for e in edges:
s, t, w = e
# check if nodes are in the cycle or not
new_s = s if s not in cycle else new_node
new_t = t if t not in cycle else new_node
if new_s != new_t:
# Add the edge with the new nodes
new_incoming[new_t].append((new_s, new_t, w))
# Recursively compute the cost for the new graph
cost = edmonds(new_incoming, root)
return cost
result = edmonds(incoming, root)
print(result)
if __name__ == "__main__":
main()
Explanation
Reading Input: The graph is read and stored with edges listed both as part of the edge list and incoming edges for each node.