Minimum Spanning Arborescence
Time limit2sMemory limit1024 MB
Find the minimum total weight of a spanning arborescence rooted at r in a directed weighted graph, or report -1 if none exists.
- Level
Hard8 of 10
- Topics
- Minimum spanning tree, Graph, Union-find, Heap
- Solved
- No attempts yet
Problem
An arborescence is a directed graph that satisfies the following conditions.
- There is a vertex such that for every vertex , there is a path from to . This vertex is called the root node.
- Let be the set of edges of with their directions removed. The undirected graph is a tree.
You are given a directed graph with weighted edges and no cycles, along with a vertex . Each vertex is numbered from to . You need to find the minimum total edge weight among all spanning arborescences whose root node is .
The Chu-Liu-Edmonds' algorithm runs in , and Tarjan's improvement of it runs in . However, this problem does not ask you to use that algorithm.
Find the minimum total weight of a spanning arborescence rooted at in the given graph.
Input
The first line contains the number of vertices , the number of edges , and the root vertex , separated by spaces. (; ; )
From the second line through the -th line, each line contains three integers , , and , separated by spaces. This means there is an edge from vertex to vertex with weight . (; ; )
Any two vertices are connected by at most one edge in the given graph, and the graph has no cycles.
Output
Print the minimum total edge weight of a spanning arborescence rooted at . If no spanning arborescence rooted at exists, print -1.