#P12007. Minimizing Dissonance in the Wind Ensemble
Minimizing Dissonance in the Wind Ensemble
Minimizing Dissonance in the Wind Ensemble
Consider a wind ensemble with students numbered from to . Before Takishō arrives, there are already established cooperation relations. Each relation is given by three integers , , and , meaning that when either student or student plays, the other can follow and finish his part exactly time units later. If two students do not have a direct relation, they may still cooperate indirectly through a sequence of relations; the total error time between them is defined as the sum of the waiting times along the chain.
Now the ensemble is in utter disarray! To solve this, Takishō has special training schemes available. The -th scheme lets any two members establish a new cooperation relation and guarantees that after training the two can cooperate in time units. Once all training schemes are applied, the overall coordination error (or dissonance) is defined as the sum over all pairs of the minimum error time between and . (If some pair of students cannot cooperate in the final network, the training plan is considered invalid.) To qualify for the national league, Takishō wants to choose the training schemes so that the dissonance is minimized. Compute the optimal dissonance modulo .
More formally, suppose the fixed relations form a forest (each relation is represented by an edge with weight ) and let the training schemes, with values , be used to add edges among the components. In the final spanning tree (with edges) the dissonance is [ \sum_{\text{edge } e}; (\text{edge weight})\times (\text{# of pairs separated by } e), \quad\text{where the number of separated pairs by edge } e \text{ is } x\times (n-x) \text{ if one side of } e \text{ has } x \text{ nodes}. ]
The fixed edges are predetermined. You are allowed to choose any training edges among different components; furthermore, you can assign the training schemes arbitrarily to these edges. It can be shown that an optimal strategy always exists.
Your task is to output the minimum possible dissonance modulo .
inputFormat
The first line contains two integers and (, ).
Then follow lines. Each of these lines contains three integers , , and (, ), representing an existing cooperation relation between students and with error time . It is guaranteed that these relations form a forest (i.e. there are no cycles).
The next line contains integers (), representing the time required for cooperation if a training scheme is used.
outputFormat
Output a single integer: the minimum possible dissonance modulo .
sample
4 2
1 2 1
3 4 2
3
15