#P17B. Hierarchy
Hierarchy
题目描述
小 的公司有 个员工,每个员工 有一个初始的权值 ,每一个员工有且只有一个上司。 有 条申请,每个申请由三个数 ,, 构成,代表将 任命为 的上司所需要的花费为,同时必须保证 。试求使每个员工(顶头上司除外)都有且只有一个上司所花费的最小代价。
输入格式
第一行 ,第二行 个数表示 ,第三行 ,之后 行每行三个数表示 ,,。
输出格式
一个整数表示最小代价,若无解则输出 -1
。
说明/提示
数据规模与约定
,, ,, 。
题目描述
Nick's company employed people. Now Nick needs to build a tree hierarchy of «supervisor-surbodinate» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are applications written in the following form: «employee is ready to become a supervisor of employee at extra cost ». The qualification of each employee is known, and for each application the following is true: .
Would you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it.
输入格式
The first input line contains integer ( ) — amount of employees in the company. The following line contains space-separated numbers ( )— the employees' qualifications. The following line contains number ( ) — amount of received applications. The following lines contain the applications themselves, each of them in the form of three space-separated numbers: , and ( , ). Different applications can be similar, i.e. they can come from one and the same employee who offered to become a supervisor of the same person but at a different cost. For each application .
输出格式
Output the only line — the minimum cost of building such a hierarchy, or -1 if it is impossible to build it.
样例 #1
样例输入 #1
4
7 2 3 1
4
1 2 5
2 4 1
3 4 1
1 3 5
样例输出 #1
11
样例 #2
样例输入 #2
3
1 2 3
2
3 1 2
3 1 3
样例输出 #2
-1
提示
In the first sample one of the possible ways for building a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the minimum total cost. In the second sample it is impossible to build the required hierarchy, so the answer is -1.
相关
在以下作业中: