Given a DAG of book-ordering constraints, choose how many days to shorten each book so all finish within X, minimizing printing plus shortening cost.
Hard8Dynamic programmingGraphTopological sortBinary searchNo attempts yetTime limit10sMemory limit512 MBA publisher has to print N books within X days. Books can normally be printed in parallel, but the publisher puts ordering restrictions on some of them. Volume 2 of a series cannot start printing before volume 1 is published, for instance. There are M restrictions, each given as a pair (u,v), which means that printing of book v can start only after book u is published.
Printing book i takes Ai days and costs Ci. With extra resources on the presses, the printing time of book i can be cut by Ri days down to Ai−Ri days, subject to Ai−Ri≥Bi. Every day cut off costs an extra Di.
Days are counted from day 0. If book i starts on day Si, its printing ends on day Si+(Ai−Ri)−1. A restriction (u,v) means Sv≥Su+(Au−Ru), and every book must satisfy Si+(Ai−Ri)≤X.
The total cost is the sum of the printing costs plus the sum of the shortening costs, ∑iCi+∑iDiRi. Find the minimum total cost of printing all N books within X days.
The first line holds the number of test cases T. Each test case has the following format.
N X
A1 A2 ... AN
B1 B2 ... BN
C1 C2 ... CN
D1 D2 ... DN
M
u1 v1
...
uM vM
The distribution of N is as follows. 85% of the test cases have N≤30, three of them have N=200, and the rest have 30≤N≤100.
Print one line per test case. The line starts with Case k: , where k is the test case number counting from 1. Append Impossible if the N books cannot all be printed within X days, otherwise append the minimum total cost.
The figure below shows an example with three books. Book 1 takes 5 days and cannot be shortened. Book 2 and book 3 take 4 days each, and book 3 can start only after book 2 is published. Without extra resources all three books take 8 days. Cutting 2 days from book 2 and 1 day from book 3 finishes everything in 5 days.
