#P13141. [GCJ 2018 #1B] Transmutation
[GCJ 2018 #1B] Transmutation
题目描述
You are the most skilled alchemist of a country that considers metals such as gold, platinum, and silver to be uninteresting, but highly values lead. There are metals known in the world; lead is metal number 1 on your periodic table. Your country's leader has asked you to use the metal in the treasury to make as much lead as possible.
For each metal (including lead), you know exactly one formula that lets you create one gram of that metal by destroying one gram each of two ingredient metals. (If you are wondering about the principle of mass conservation, the other gram is lost in useless waste products.) The formulas do not work with partial grams. However, you can use each formula as often as you would like (or not at all), as long as you have the required ingredients each time.
If you make optimal choices, what is the largest total amount of lead you can end up with? Note that it is possible that you may have some metals other than lead left over after you are done.
输入格式
The first line of the input gives the number of test cases, . test cases follow. Each begins with one line with an integer : the number of metals known in the world. Then there are more lines with two integers and each; the -th of these lines indicates that you can create one gram of metal by destroying one gram of metal and one gram of metal . Finally, there is one line with integers $\mathbf{G}_{1}, \mathbf{G}_{2}, \ldots, \mathbf{G}_{\mathbf{M}} ; \mathbf{G}_{\mathbf{i}}$ is the number of grams of metal in the treasury. Lead is metal 1 .
输出格式
For each test case, output one line containing Case #x: y
, where is the test case number (starting from 1) and is the largest amount of lead, in grams, that you can end up with.
3
3
2 3
1 3
1 2
5 2 3
5
3 4
3 4
4 5
3 5
1 3
0 8 6 2 4
4
3 4
2 3
2 3
2 3
0 1 1 0
Case #1: 7
Case #2: 4
Case #3: 0
提示
Sample Explanation
In Sample Case #1, the optimal strategy is to use grams of metals and to produce more grams of lead, for a total of grams of lead.
In Sample Case #2, the optimal strategy is to first use grams of metal and grams of metal to produce grams of metal , and then use grams of metal and grams of metal to produce grams of lead. Note that it is possible for two formulas to have the same two ingredients (you just use different alchemical techniques). Also note that not every metal is necessarily an ingredient in some other formula; in this case, metal is never an ingredient.
In Sample Case #3, note that it is possible for a metal to be used to produce itself. (Sometimes the laws of alchemy may be silly!) Unfortunately, it is not possible to produce any lead in this case. Note that since formulas only work on single-gram quantities, you cannot, for example, use grams of each of metals and to create grams of metal , and then use grams of each of metals and to create grams of lead.
Limits
- .
- , for all .
Test set 1 (15 Pts,Visible)
- .
- , for all .
Test set 2 (18 Pts, Hidden)
- .
- , for all .
Test set 3 (12 Pts, Hidden)
- .
- , for all .