title |
---|
DSA2: PA4: Mixing Magical Reagents |
{style="width:25%;float:right;margin-left:20px;border-radius:10px"}
It's course registration time! While choosing electives, you came across a rather interesting one -- Mixing Magical Reagents (and Computing), taught by "Staff" -- meaning someone yet to be hired. Somehow this course counts as a computing elective. Curious to learn more, you sign up for the course. Like all CS electives, the wait list size is already larger than the number of people enrolled -- many times larger, in fact. But somehow you end up in the course, and quickly find out that it is more similar to a chemistry mixing free-for-all than an actual computing elective. The first assignment in that class is how to optimize the mixing of magical reagents -- the physical components needed to cast spells.
You find that you have abc
, which might mean to mix Ashes of mistletoe with Bone dust with a caterpillar Cocoon. Reagent strings can be as long as
Mixing reagents uses some magical energy -- a cost, if you will. Each pair of reagents has a separate cost, and that cost varies depending on the order that you mix them together. So the cost adding
You will be provided with a table that lists both the cost of mixing two reagents, as well as the resulting reagent:
a | b | c | |
---|---|---|---|
a | c:10 | a:4 | a:4 |
b | c:3 | a:4 | a:2 |
c | b:4 | b:1 | b:8 |
This means that mixing b+c will yield reagent a, and cost 2 energy. Mixing c+b will yield reagent b, and cost 1 energy. Thus, the mixing of reagents is not commutative:
Given a table in a format similar to what is above, and a list of reagents to mix, you have to determine the mixing order that minimizes the cost, as well as the resulting reagent. You cannot change the order of the reagents in the reagent string, only change the order of which ones you mix first. In other words, given the reagent string abc
as your mixing string, you have to determine which is the minimum cost between
The first line of the input contains an integer
The first line of each test case contains an integer
The next c:3 a:4 a:2
(the middle row in the table above). All costs are integers
After the table is an integer
All input is to be read from standard input.
For each mixing in each test case, there should be two values output on a single line (space separated): the resulting reagent and the minimal cost. If there are multiple paths that lead to the same minimum cost, you may print out the resulting reagent from any of them. These minimal costs -- and all partial costs computed in the process -- are guaranteed to fit into a 32-bit singed integer.
All input is to be written to standard output.
This input below is available in the sample.in file. The first test case therein corresponds to the example above.
2
3
c:10 a:4 a:4
c:3 a:4 c:2
b:4 b:1 b:8
3
abc
cab
abcabcabcabc
2
b:3 b:5
a:6 b:2
1
abba
Your program will be run as follows, if in Python:
$ python pa4.py < sample.in
Or, if in Java:
$ java PA4 < sample.in
The <
will provide the contents of the sample.in file as standard input to your program.
a 6
a 8
a 30
b 11
Note that the second test case could have also output b 8
, as doing the two operations in either order yields a magical cost of 8. Specifically, (meaning
This needs to be a dynamic programming solution. Brute force solutions will time out with the test cases we are going to provide.
There is no skeleton code being provided for this programming assignment. You can look at PA2 for how to read in the input from standard input (but note that the input for this assignment is quite different than that for PA2).
Your source code file must be named either pa4.py
or PA4.java
.