{"problem":{"name":"E. Ralph and Mushrooms","description":{"content":"Ralph is going to collect mushrooms in the Mushroom Forest. There are _m_ directed paths connecting _n_ trees in the Mushroom Forest. On each path grow some mushrooms. When Ralph passes a path, he co","description_type":"Markdown"},"platform":"Codeforces","limit":{"time_limit":2000,"memory_limit":524288},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"CF894E"},"statements":[{"statement_type":"Markdown","content":"Ralph is going to collect mushrooms in the Mushroom Forest.\n\nThere are _m_ directed paths connecting _n_ trees in the Mushroom Forest. On each path grow some mushrooms. When Ralph passes a path, he collects all the mushrooms on the path. The Mushroom Forest has a magical fertile ground where mushrooms grow at a fantastic speed. New mushrooms regrow as soon as Ralph finishes mushroom collection on a path. More specifically, after Ralph passes a path the _i_\\-th time, there regrow _i_ mushrooms less than there was before this pass. That is, if there is initially _x_ mushrooms on a path, then Ralph will collect _x_ mushrooms for the first time, _x_ - 1 mushrooms the second time, _x_ - 1 - 2 mushrooms the third time, and so on. However, the number of mushrooms can never be less than 0.\n\nFor example, let there be 9 mushrooms on a path initially. The number of mushrooms that can be collected from the path is 9, 8, 6 and 3 when Ralph passes by from first to fourth time. From the fifth time and later Ralph can't collect any mushrooms from the path (but still can pass it).\n\nRalph decided to start from the tree _s_. How many mushrooms can he collect using only described paths?\n\n## Input\n\nThe first line contains two integers _n_ and _m_ (1 ≤ _n_ ≤ 106, 0 ≤ _m_ ≤ 106), representing the number of trees and the number of directed paths in the Mushroom Forest, respectively.\n\nEach of the following _m_ lines contains three integers _x_, _y_ and _w_ (1 ≤ _x_, _y_ ≤ _n_, 0 ≤ _w_ ≤ 108), denoting a path that leads from tree _x_ to tree _y_ with _w_ mushrooms initially. There can be paths that lead from a tree to itself, and multiple paths between the same pair of trees.\n\nThe last line contains a single integer _s_ (1 ≤ _s_ ≤ _n_) — the starting position of Ralph.\n\n## Output\n\nPrint an integer denoting the maximum number of the mushrooms Ralph can collect during his route.\n\n[samples]\n\n## Note\n\nIn the first sample Ralph can pass three times on the circle and collect 4 + 4 + 3 + 3 + 1 + 1 = 16 mushrooms. After that there will be no mushrooms for Ralph to collect.\n\nIn the second sample, Ralph can go to tree 3 and collect 8 mushrooms on the path from tree 1 to tree 3.","is_translate":false,"language":"English"},{"statement_type":"Markdown","content":"Ralph 计划在蘑菇森林中采集蘑菇。\n\n蘑菇森林中有 #cf_span[m] 条有向路径连接 #cf_span[n] 棵树。每条路径上都生长着一些蘑菇。当 Ralph 经过一条路径时，他会收集该路径上所有的蘑菇。蘑菇森林拥有神奇的肥沃土壤，蘑菇以惊人的速度再生。具体而言，在 Ralph 第 #cf_span[i] 次经过某条路径后，该路径上再生的蘑菇数量比上次经过前少 #cf_span[i] 个。也就是说，如果一条路径初始有 #cf_span[x] 个蘑菇，那么 Ralph 第一次经过时收集 #cf_span[x] 个蘑菇，第二次经过时收集 #cf_span[x - 1] 个蘑菇，第三次经过时收集 #cf_span[x - 1 - 2] 个蘑菇，依此类推。但蘑菇数量永远不会低于 #cf_span[0]。\n\n例如，若一条路径初始有 #cf_span[9] 个蘑菇，则 Ralph 第一次到第四次经过时分别可收集 #cf_span[9]、#cf_span[8]、#cf_span[6] 和 #cf_span[3] 个蘑菇。从第五次及以后，Ralph 无法再从该路径上收集任何蘑菇（但仍可经过）。\n\nRalph 决定从树 #cf_span[s] 出发。他仅通过上述路径最多能收集多少蘑菇？\n\n第一行包含两个整数 #cf_span[n] 和 #cf_span[m]（#cf_span[1 ≤ n ≤ 106]，#cf_span[0 ≤ m ≤ 106]），分别表示树的数量和有向路径的数量。\n\n接下来的 #cf_span[m] 行每行包含三个整数 #cf_span[x]、#cf_span[y] 和 #cf_span[w]（#cf_span[1 ≤ x, y ≤ n]，#cf_span[0 ≤ w ≤ 108]），表示一条从树 #cf_span[x] 指向树 #cf_span[y] 的路径，初始有 #cf_span[w] 个蘑菇。可能存在从一棵树到自身的路径，以及连接相同两棵树的多条路径。\n\n最后一行包含一个整数 #cf_span[s]（#cf_span[1 ≤ s ≤ n]）—— Ralph 的起始位置。\n\n请输出一个整数，表示 Ralph 在其路线中最多能收集的蘑菇数量。\n\n在第一个样例中，Ralph 可以在环路上经过三次，收集 #cf_span[4 + 4 + 3 + 3 + 1 + 1 = 16] 个蘑菇。之后该路径上将不再有蘑菇可供采集。\n\n在第二个样例中，Ralph 可以前往树 #cf_span[3]，并在从树 #cf_span[1] 到树 #cf_span[3] 的路径上收集 #cf_span[8] 个蘑菇。\n\n## Input\n\n第一行包含两个整数 #cf_span[n] 和 #cf_span[m]（#cf_span[1 ≤ n ≤ 106]，#cf_span[0 ≤ m ≤ 106]），分别表示树的数量和有向路径的数量。接下来的 #cf_span[m] 行每行包含三个整数 #cf_span[x]、#cf_span[y] 和 #cf_span[w]（#cf_span[1 ≤ x, y ≤ n]，#cf_span[0 ≤ w ≤ 108]），表示一条从树 #cf_span[x] 指向树 #cf_span[y] 的路径，初始有 #cf_span[w] 个蘑菇。可能存在从一棵树到自身的路径，以及连接相同两棵树的多条路径。最后一行包含一个整数 #cf_span[s]（#cf_span[1 ≤ s ≤ n]）—— Ralph 的起始位置。\n\n## Output\n\n请输出一个整数，表示 Ralph 在其路线中最多能收集的蘑菇数量。\n\n[samples]\n\n## Note\n\n在第一个样例中，Ralph 可以在环路上经过三次，收集 #cf_span[4 + 4 + 3 + 3 + 1 + 1 = 16] 个蘑菇。之后该路径上将不再有蘑菇可供采集。\n\n在第二个样例中，Ralph 可以前往树 #cf_span[3]，并在从树 #cf_span[1] 到树 #cf_span[3] 的路径上收集 #cf_span[8] 个蘑菇。","is_translate":true,"language":"Chinese"},{"statement_type":"Markdown","content":"Let $ G = (V, E) $ be a directed multigraph with $ |V| = n $ vertices and $ |E| = m $ edges. Each edge $ e = (u, v) \\in E $ has an initial mushroom count $ w_e \\geq 0 $.\n\nFor each edge $ e $, if traversed $ k $ times, the total mushrooms collected from $ e $ is:\n\n$$\n\\sum_{i=1}^{k} \\max\\left(0, w_e - \\frac{i(i-1)}{2}\\right)\n$$\n\nDefine the function $ f_e(k) = \\sum_{i=1}^{k} \\max\\left(0, w_e - \\frac{i(i-1)}{2}\\right) $, which gives the total mushrooms collected from edge $ e $ when traversed $ k $ times.\n\nLet $ x_e \\in \\mathbb{Z}_{\\geq 0} $ denote the number of times edge $ e $ is traversed.\n\nRalph starts at vertex $ s $. The sequence of traversals must form a walk starting at $ s $, and the flow conservation constraint must hold at every vertex: for each vertex $ v \\in V $,\n\n$$\n\\sum_{e \\in \\text{in}(v)} x_e = \\sum_{e \\in \\text{out}(v)} x_e\n$$\n\nexcept for the start vertex $ s $, where:\n\n$$\n\\sum_{e \\in \\text{out}(s)} x_e = \\sum_{e \\in \\text{in}(s)} x_e + 1\n$$\n\nThe objective is to maximize the total mushrooms collected:\n\n$$\n\\max \\sum_{e \\in E} f_e(x_e)\n$$\n\nsubject to the above flow constraints and $ x_e \\in \\mathbb{Z}_{\\geq 0} $ for all $ e \\in E $.","is_translate":false,"language":"Formal"}],"meta":{"iden":"CF894E","tags":["dp","graphs"],"sample_group":[["2 2\n1 2 4\n2 1 4\n1","16"],["3 3\n1 2 4\n2 3 3\n1 3 8\n1","8"]],"created_at":"2026-03-03 11:00:39"}}