luogu#P4716. 【模板】最小树形图
【模板】最小树形图
Background
This is a template problem.
Problem Description
Given a directed graph with nodes and directed edges. Find a minimum arborescence rooted at node , and output the sum of the weights of all edges in the minimum arborescence. If there is no minimum arborescence rooted at , output .
Input Format
The first line contains three integers , with the same meanings as described in the statement.
The next lines each contain three integers , indicating that there is a directed edge from to with weight .
Output Format
If the original graph has a minimum arborescence rooted at , output the sum of the weights of all edges in the minimum arborescence; otherwise output .
4 6 1
1 2 3
1 3 1
4 1 2
4 2 2
3 2 1
3 4 1
3
4 6 3
1 2 3
1 3 1
4 1 2
4 2 2
3 2 1
3 4 1
4
4 6 2
1 2 3
1 3 1
4 1 2
4 2 2
3 2 1
3 4 1
-1
Hint
Sample Explanation
The minimum arborescence contains edges , , and , with total weight .
Sample Explanation
The minimum arborescence contains edges , , and , with total weight .
Sample Explanation
A minimum arborescence cannot be formed, so output .
Constraints
For all testdata, , , .
Translated by ChatGPT 5