#P4501. [ZJOI2018] 胖
[ZJOI2018] 胖
Description
Cedyks is a rich boy. He lives in the famous The Place (palace).
Cedyks is a hard-working boy. Every day he solves different problems to train his The Salt (soul).
One day, he plans to build a wall around his palace. There are watchtowers on the wall. You can regard the wall as a line segment, and the watchtowers as points on the segment, where and are the two endpoints of the wall. The distance between watchtower and watchtower is , and the road between them is bidirectional.
The wall is built soon. Now Cedyks starts planning the roads from his palace to the wall. Because of the problem title, Cedyks decides to measure a construction plan by the sum of the shortest path lengths from his palace to every watchtower.
Now Cedyks has design plans. In the -th plan, bidirectional roads will be built between the palace and the watchtowers. The -th road connects to watchtower and has length .
Computing the sum of shortest paths to every watchtower is heavy work. Originally, Cedyks wanted to use the well-known SPFA algorithm, but because his butter (buffer) is too small, he has to use the primitive Bellman-Ford algorithm instead. The process is roughly as follows:
- Define the palace as node , and the -th watchtower as node . A bidirectional edge is a bidirectional road connecting and . Let be the distance array. Initially, .
- Let the auxiliary array . For each edge in order, relax it: , .
- Let be the number of positions where and differ. That is, let , then . If , it means is the final shortest path result, and the algorithm ends. Otherwise, set and go back to step 2.
Because there are too many design plans to compute, Cedyks hires some people to help. To prevent them from slacking off with made-up data, he defines the checksum value of a design plan as the sum of every time the Bellman-Ford algorithm enters step 3 when running on this plan. He will ask several hired people to compute the same plan and compare the checksum values they provide.
You are one of the laborers hired by Cedyks. Being smart, you find that in this situation, computing the sum of shortest path lengths is very easy. However, since you have to obey, you still need to compute the checksum value for each plan to report.
Input Format
The first line contains two integers , representing the number of watchtowers and the number of design plans.
The next line contains numbers , representing the length of the road between watchtower and watchtower .
Then follow lines, each describing a design plan. The first integer is the number of roads in the plan. Then follow pairs , each representing an edge from the palace to watchtower with length .
Output Format
For each design plan, output one line with one integer, representing the checksum value.
5 5
2 3 1 4
1 2 2
2 1 1 4 10
3 1 1 3 1 5 1
3 1 10 2 100 5 1
5 1 1 2 1 3 1 4 1 5 1
5
8
5
8
5
Hint
Sample Explanation
For the first design plan, the changes of at each stage are:
- $[0,10^{18},10^{18},10^{18},10^{18},10^{18}] \rightarrow [0,10^{18},2,10^{18},10^{18},10^{18}] \rightarrow$ $[0,4,2,5,10^{18},10^{18}] \rightarrow [0,4,2,5,6,10^{18}] \rightarrow [0,4,2,5,6,10]$.
So the checksum value is .
For the second design plan, the changes of at each stage are:
- $[0,10^{18},10^{18},10^{18},10^{18},10^{18}] \rightarrow [0,1,10^{18},10^{18},10,10^{18}] \rightarrow$ $[0,1,3,11,10,14] \rightarrow [0,1,3,6,10,14] \rightarrow [0,1,3,6,7,14] \rightarrow [0,1,3,6,7,11]$.
So the checksum value is .
For the third design plan, the changes of at each stage are:
- $[0,10^{18},10^{18},10^{18},10^{18},10^{18}] \rightarrow [0,1,10^{18},1,10^{18},1] \rightarrow [0,1,3,1,2,1]$。
So the checksum value is .
For the fourth design plan, the changes of at each stage are:
- $[0,10^{18},10^{18},10^{18},10^{18},10^{18}] \rightarrow [0,10,100,10^{18},10^{18},1] \rightarrow$ $[0,10,12,103,5,1] \rightarrow [0,10,12,6,5,1] \rightarrow [0,10,9,5,1]$。
So the checksum value is .
For the fifth design plan, the changes of at each stage are:
- $[0,10^{18},10^{18},10^{18},10^{18},10^{18}] \rightarrow [0,1,1,1,1,1]$
So the checksum value is .
Constraints
| Test point | Other constraints | |||
|---|---|---|---|---|
| 1,2 | None | |||
| 3,4 | ||||
| 5,6 | ||||
| 7,8,9,10 | None | |||
For of the testdata, it is guaranteed that within each design plan, all are pairwise distinct and .
For of the testdata, it is guaranteed that .
Thanks to @Xeonacid for providing the statement.
Translated by ChatGPT 5
京公网安备 11011102002149号