Mana Collection
Time limit5sMemory limit1024 MB
Given a directed graph of mana pools with collection rates, find the most mana collectable in s seconds while ending at pool e, for each query.
- Level
Hard9 of 10
- Topics
- Dynamic programming, Bit manipulation, Shortest path, Divide and conquer
- Solved
- No attempts yet
Problem
Bessie has recently taken an interest in magic and needs to collect mana for a very important spell. Bessie has () mana pools, the th of which accumulates mana per second (). The pools are linked by a collection of () directed edges , meaning that she can travel from to in seconds (, , ). Whenever Bessie is present at a pool, she can collect all the mana stored there, emptying it. At time , all mana pools are empty, and Bessie can start at any pool.
Answer () queries. Each query gives two integers and (, ). For each query, find the maximum amount of mana Bessie can collect in seconds if she must be at mana pool at the end of the th second.
Input
The first line contains and .
The next line contains .
The next lines each contain , , . No ordered pair appears more than once.
The next line contains .
The next lines each contain two integers and .
Output
Print lines, one for each query.