Alice and Bob are playing a game on a graph.
The graph has int N nodes, and M directed edges.
The graph is described by the int from,to,cost.
The i-th edge is a directed edge that connects node from[i] to node to[i] and has weight cost[i].
This graph may have self loops or multiple edges.
Each node has at least one outgoing edge.
Alice is currently at at node 0.
She wishes to travel to node N-1.
Bob initially has K tokens.
At each node along Alice's path, she will first announce which outgoing edge she intends to traverse.
If Bob wishes and if he still has a token, he may spend one token and choose a different outgoing edge from Alice's current node.
Alice will then be forced to travel along the edge chosen by Bob and incur the cost of this edge.
If Bob does not use a token, Alice travels along the edge she announced, incurring the cost of the edge.
Alice knows how many tokens Bob has remaining at all times.
The game ends when Alice reaches node N-1, or she traverses an edge 1,000,000,000 times without reaching the node N-1.
Alice wishes to minimize her travel time.
Bob will use his tokens in such a way that Alice cannot reach node N-1 if it is possible.
Otherwise, he will use them to maximize the total cost that Alice incurs before reaching node N-1, and Alice will always choose an option to minimize the total cost.
If it is not possible for Alice to make it to node N-1, return -1.
Otherwise, compute and return the minimum time that Alice can guarantee to reach the node N-1.
|Parameters:||int, int, int, int, int|
|Method signature:||long minPath(int N, int K, int from, int to, int cost)|
|(be sure your method is public)|
|-||N will be between 2 and 1,000, inclusive.|
|-||from,to,cost will each contain between 1 and 2,500 elements, inclusive.|
|-||from,to,cost will contain the same number of elements.|
|-||Each element of from,to will be between 0 and N-1, inclusive.|
|-||Each element of cost will be between 0 and 1,000,000, inclusive.|
|-||Each integer between 0 and N-1, inclusive, will appear at least once in the array from.|
|-||K will be between 0 and 1,000, inclusive.|
When Alice is at node 0, she will attempt to travel to node 1 using the first edge.
Bob will do nothing.
When Alice is at node 1, she will attempt to travel to node 2 using the third edge.
If Bob has a token, Bob will make her go back to node 0 instead using the second edge.
Thus, Alice has to travel the cycle 0->1->0 1000 times before she can successfully make it to node 2.
This will yield a total cost of 5004.
In this case, Alice's optimal strategy is to attempt to go to node 3 directly from node 0. Bob's optimal response will be not use a token, allowing Alice to reach node 3.
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2010, TopCoder, Inc. All rights reserved.