eolymp
bolt
Try our new interface for solving problems
Problems

Replica Placement

Replica Placement

Topsky wants to build a content delivery network. It contains an origin server and some mirror servers as shown in figure \textbf{1}. Original data is put at the origin server (root in figure \textbf{1}) while replicas are put at some of the mirror servers (node \textbf{2} and \textbf{5} in figure \textbf{1}). When a node issues a request of data, it will try to find its destination in following steps. \begin{enumerate} \item Check if there is a replica at its own place. If yes, the request meets. Otherwise do step \textbf{2}. \item Forward the request to its parent, and let its parent do step \textbf{1}. \end{enumerate} \includegraphics{https://static.e-olymp.com/content/74/74dc1d0e865c9bc90e76b6fa575090a3ee7fcb83.jpg} The cost of meeting the request \textbf{C}(\textbf{v}) is defined as the sum of weight of the edges along the road. If \textbf{C}(\textbf{v}) is not greater than an upper bound \textbf{Q}(\textbf{v}), then the retrieval cost is satisfied. Topsky further assumes a nonnegative cost \textbf{S}(\textbf{v}) which means the cost of storing data at node \textbf{v}. Note that the origin server is special, the cost of storing data at origin server is \textbf{0}. Now Topsky wants to find the way of replicas placement such the retrieval cost of all nodes are satisfied while the total storage cost is minimal. \InputFile The first line contains a single integer \textbf{t} (\textbf{t} ≤ \textbf{20}), indicating the number of test cases. Each case begins with an integer \textbf{n} (\textbf{1 }≤ \textbf{n} ≤ \textbf{1000}) that indicates the number of servers. Then \textbf{n} lines follow. Each line contains four integers \textbf{F_v} (\textbf{0} ≤ \textbf{F_v} ≤ \textbf{n}), \textbf{Q_v}, \textbf{S_v} and \textbf{W_v} (\textbf{0} ≤ \textbf{Q_v}, \textbf{S_v}, \textbf{W_v} ≤ \textbf{10^5}). In line \textbf{i} (\textbf{1} ≤ \textbf{i} ≤ \textbf{n}), \textbf{F_v} is the father of node \textbf{i} (if \textbf{F_v} is \textbf{0}, it means node \textbf{i} is the origin server, \textbf{Q_v} is \textbf{-1}, \textbf{S_v} is \textbf{0} and \textbf{W_v} is \textbf{0}), \textbf{Q_v} is upper bound of retrieval cost, \textbf{S_v} is storing cost of node \textbf{i} and \textbf{W_v} is weight of this edge between node \textbf{i} and node \textbf{F_v}. \OutputFile For each test case, output the minimal storage cost in one line.
Time limit 1 second
Memory limit 64 MiB
Input example #1
AGCT
GCAT
Output example #1
3
1 2
2 3
3 3