#P9706. 「TFOI R1」Ride the Wind and Waves

    ID: 10663 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DPO2优化前缀和基环树差分

「TFOI R1」Ride the Wind and Waves

Background

Professor Z is the teacher of Class C.

Professor Z recently discovered a magical phenomenon: all of his students secretly have a crush, but none of them dares to confess.

As someone who has been through it, Professor Z of course understands the most real and pure thoughts in each student's heart, as well as what they believe to be love. Professor Z recalled his first love, Jiao Tailang (pinyin), and he did not want his students to lose color in their youth. So, Professor Z took the risk of being fired and主动 helped the students express their feelings.

Then Professor Z was fired.

Problem Description

There is an inward directed unicyclic graph with nn nodes (weakly connected is guaranteed). Each edge has a weight. There is also a fixed parameter kk.

Since the graph is inward, a node xx may have nodes that it cannot reach directly. However, we may reverse some directed edges in the graph so that xx can reach every node. If a node xx needs to reverse at least kk edges in order to reach yy, then yy is called a "Ride the Wind and Waves" node of xx. After reversing the minimum number of edges so that xx can reach yy, on the shortest path from xx to yy, define F(x,y)F(x, y) as the sum of weights of the non-reversed edges, and R(x,y)R(x, y) as the sum of weights of the reversed edges.

If yy is a "Ride the Wind and Waves" node of xx, then there is a value G(x,y)G(x, y) representing the wave value from xx to yy, defined as G(x,y)=F(x,y)×R(x,y)G(x, y) = F(x, y) \times R(x, y).

For each node ii, output the value of G(i,y)\sum G(i, y), where yy ranges over all "Ride the Wind and Waves" nodes of ii.

Input Format

The first line contains two positive integers n,kn, k, representing the size of the unicyclic graph and the comparison parameter.

The next nn lines each contain three positive integers u,v,wu, v, w, indicating that there is an edge (u,v,w)(u, v, w) in the graph, where the starting node is uu, the ending node is vv, and the weight is ww.

Output Format

Output nn lines, each containing one positive integer, representing for each node the sum of wave values to all of its "Ride the Wind and Waves" nodes.

7 1
1 4 3
2 1 2
3 1 6
4 3 4
5 2 4
6 4 1
7 5 2
3
5
105
160
9
176
11
7 1
1 2 3
2 3 2
3 1 2
4 1 3
5 4 2
6 2 1
7 6 4
18
32
46
36
48
40
72

Hint

Sample Explanation #1

Take the answer for node 33 as an example. The unicyclic graph looks like the figure below:

We can see that 2,5,6,72, 5, 6, 7 are "Ride the Wind and Waves" nodes of 33. Compute the answer:

  • G(3,2)=6×2=12G(3, 2) = 6 \times 2 = 12.

  • G(3,5)=6×6=36G(3, 5) = 6 \times 6 = 36.

  • G(3,6)=9×1=9G(3, 6) = 9 \times 1 = 9.

  • G(3,7)=6×8=48G(3, 7) = 6 \times 8 = 48.

So G(3,j)=12+36+9+48\sum G(3, j) = 12 + 36 + 9 + 48, and the answer is 105105.

Constraints

This problem uses bundled testdata.

  • Subtask 1 (5 points): 1n101 \leqslant n \leqslant 10, includes special property.
  • Subtask 2 (10 points): 1n50001 \leqslant n \leqslant 5000, includes special property.
  • Subtask 3 (25 points): 1n1051 \leqslant n \leqslant 10^5, includes special property.
  • Subtask 4 (60 points): 1n1061 \leqslant n \leqslant 10^6, no special restrictions.

Special property: the number of nodes on the cycle is guaranteed to be within 10310^3.

For all testdata, 1n1061 \leqslant n \leqslant 10^6, 1k101 \leqslant k \leqslant 10, and the answer is guaranteed not to exceed 101810^{18}.

Translated by ChatGPT 5