CF1266D.Decreasing Debts

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There are nn people in this world, conveniently numbered 11 through nn. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let d(a,b)d(a,b) denote the debt of aa towards bb, or 00 if there is no such debt.

Sometimes, this becomes very complex, as the person lending money can run into financial troubles before his debtor is able to repay his debt, and finds himself in the need of borrowing money.

When this process runs for a long enough time, it might happen that there are so many debts that they can be consolidated. There are two ways this can be done:

  1. Let d(a,b)>0d(a,b) \gt 0 and d(c,d)>0d(c,d) \gt 0 such that a≠ca \neq c or b≠db \neq d. We can decrease the d(a,b)d(a,b) and d(c,d)d(c,d) by zz and increase d(c,b)d(c,b) and d(a,d)d(a,d) by zz, where 0<z≤min⁡(d(a,b),d(c,d))0 \lt z \leq \min(d(a,b),d(c,d)).
  2. Let d(a,a)>0d(a,a) \gt 0. We can set d(a,a)d(a,a) to 00.

The total debt is defined as the sum of all debts:

Sigma_d=sum_a,bd(a,b)\\Sigma\_d = \\sum\_{a,b} d(a,b)

Your goal is to use the above rules in any order any number of times, to make the total debt as small as possible. Note that you don't have to minimise the number of non-zero debts, only the total debt.

世界上有 nn 个人,编号分别为 11 到 nn。他们使用“布尔”(burles)作为货币来购买商品和服务。有时,某个人可能没有足够的钱来购买他想要或需要的东西,因此会向他人借钱,并计划日后连本带息偿还。令 d(a,b)d(a,b) 表示 aa 欠 bb 的债务;若 aa 不欠 bb 债务,则 d(a,b)=0d(a,b) = 0。

这种借贷关系有时会变得非常复杂:放贷者可能在借款人还清债务之前就陷入财务困境,从而不得不自己去向他人借款。

当这一过程持续足够长的时间后,债务网络可能变得极为庞大,此时便可以对债务进行整合(consolidation)。整合可通过以下两种方式进行:

  1. 设 d(a,b)>0d(a,b) > 0 且 d(c,d)>0d(c,d) > 0,满足 a≠ca \neq c 或 b≠db \neq d。我们可以将 d(a,b)d(a,b) 和 d(c,d)d(c,d) 同时减少 zz,并将 d(c,b)d(c,b) 和 d(a,d)d(a,d) 同时增加 zz,其中 0<z≤min⁡(d(a,b),d(c,d))0 < z \leq \min(d(a,b),d(c,d))。
  2. 设 d(a,a)>0d(a,a) > 0。我们可以将 d(a,a)d(a,a) 置为 00。

总债务定义为所有债务之和:

Σd=∑a,bd(a,b)\Sigma_d = \sum_{a,b} d(a,b)

你的目标是:以任意顺序、任意次数地应用上述两条规则,使得总债务尽可能小。注意:你无需最小化非零债务项的个数,只需最小化总债务值。

输入格式

The first line contains two space separated integers nn (1≤n≤1051 \leq n \leq 10^5) and mm (0≤m≤3⋅1050 \leq m \leq 3\cdot 10^5), representing the number of people and the number of debts, respectively.

mm lines follow, each of which contains three space separated integers uiu_i, viv_i (1≤ui,vi≤n,ui≠vi1 \leq u_i, v_i \leq n, u_i \neq v_i), did_i (1≤di≤1091 \leq d_i \leq 10^9), meaning that the person uiu_i borrowed did_i burles from person viv_i.

第一行包含两个以空格分隔的整数 nn(1≤n≤1051 \leq n \leq 10^5)和 mm(0≤m≤3⋅1050 \leq m \leq 3\cdot 10^5),分别表示人数和债务数量。

接下来有 mm 行,每行包含三个以空格分隔的整数 uiu_i、viv_i(1≤ui,vi≤n, ui≠vi1 \leq u_i, v_i \leq n,\ u_i \neq v_i)和 did_i(1≤di≤1091 \leq d_i \leq 10^9),表示第 uiu_i 个人向第 viv_i 个人借了 did_i 卢布。

输出格式

On the first line print an integer m′m' (0≤m′≤3⋅1050 \leq m' \leq 3\cdot 10^5), representing the number of debts after the consolidation. It can be shown that an answer always exists with this additional constraint.

After that print m′m' lines, ii-th of which contains three space separated integers ui,vi,diu_i, v_i, d_i, meaning that the person uiu_i owes the person viv_i exactly did_i burles. The output must satisfy 1≤ui,vi≤n1 \leq u_i, v_i \leq n, ui≠viu_i \neq v_i and 0<di≤10180 \lt d_i \leq 10^{18}.

For each pair i≠ji \neq j, it should hold that ui≠uju_i \neq u_j or vi≠vjv_i \neq v_j. In other words, each pair of people can be included at most once in the output.

第一行输出一个整数 m′m'(0≤m′≤3⋅1050 \leq m' \leq 3\cdot 10^5),表示债务合并后的债务总数。可以证明,在该附加约束下,答案一定存在。

随后输出 m′m' 行,其中第 ii 行包含三个以空格分隔的整数 ui,vi,diu_i, v_i, d_i,表示人 uiu_i 欠人 viv_i 恰好 did_i 卢布。输出必须满足 1≤ui,vi≤n1 \leq u_i, v_i \leq n、ui≠viu_i \neq v_i 以及 0<di≤10180 \lt d_i \leq 10^{18}。

对于任意 i≠ji \neq j,需满足 ui≠uju_i \neq u_j 或 vi≠vjv_i \neq v_j。换言之,每一对人至多在输出中出现一次。

输入输出样例

  • 输入#1

    3 2
    1 2 10
    2 3 5

    输出#1

    2
    1 2 5
    1 3 5
  • 输入#2

    3 3
    1 2 10
    2 3 15
    3 1 10

    输出#2

    1
    2 3 5
  • 输入#3

    4 2
    1 2 12
    3 4 8

    输出#3

    2
    1 2 12
    3 4 8
  • 输入#4

    3 4
    2 3 1
    2 3 2
    2 3 4
    2 3 8

    输出#4

    1
    2 3 15

说明/提示

In the first example the optimal sequence of operations can be the following:

  1. Perform an operation of the first type with a=1a = 1, b=2b = 2, c=2c = 2, d=3d = 3 and z=5z = 5. The resulting debts are: d(1,2)=5d(1, 2) = 5, d(2,2)=5d(2, 2) = 5, d(1,3)=5d(1, 3) = 5, all other debts are 00;
  2. Perform an operation of the second type with a=2a = 2. The resulting debts are: d(1,2)=5d(1, 2) = 5, d(1,3)=5d(1, 3) = 5, all other debts are 00.

In the second example the optimal sequence of operations can be the following:

  1. Perform an operation of the first type with a=1a = 1, b=2b = 2, c=3c = 3, d=1d = 1 and z=10z = 10. The resulting debts are: d(3,2)=10d(3, 2) = 10, d(2,3)=15d(2, 3) = 15, d(1,1)=10d(1, 1) = 10, all other debts are 00;
  2. Perform an operation of the first type with a=2a = 2, b=3b = 3, c=3c = 3, d=2d = 2 and z=10z = 10. The resulting debts are: d(2,2)=10d(2, 2) = 10, d(3,3)=10d(3, 3) = 10, d(2,3)=5d(2, 3) = 5, d(1,1)=10d(1, 1) = 10, all other debts are 00;
  3. Perform an operation of the second type with a=2a = 2. The resulting debts are: d(3,3)=10d(3, 3) = 10, d(2,3)=5d(2, 3) = 5, d(1,1)=10d(1, 1) = 10, all other debts are 00;
  4. Perform an operation of the second type with a=3a = 3. The resulting debts are: d(2,3)=5d(2, 3) = 5, d(1,1)=10d(1, 1) = 10, all other debts are 00;
  5. Perform an operation of the second type with a=1a = 1. The resulting debts are: d(2,3)=5d(2, 3) = 5, all other debts are 00.

在第一个例子中,最优的操作序列可以如下所示:

  1. 执行一次第一类操作,其中 a=1a = 1,b=2b = 2,c=2c = 2,d=3d = 3,z=5z = 5。操作后各债务值为:d(1,2)=5d(1, 2) = 5,d(2,2)=5d(2, 2) = 5,d(1,3)=5d(1, 3) = 5,其余所有债务均为 00;
  2. 执行一次第二类操作,其中 a=2a = 2。操作后各债务值为:d(1,2)=5d(1, 2) = 5,d(1,3)=5d(1, 3) = 5,其余所有债务均为 00。

在第二个例子中,最优的操作序列可以如下所示:

  1. 执行一次第一类操作,其中 a=1a = 1,b=2b = 2,c=3c = 3,d=1d = 1,z=10z = 10。操作后各债务值为:d(3,2)=10d(3, 2) = 10,d(2,3)=15d(2, 3) = 15,d(1,1)=10d(1, 1) = 10,其余所有债务均为 00;
  2. 执行一次第一类操作,其中 a=2a = 2,b=3b = 3,c=3c = 3,d=2d = 2,z=10z = 10。操作后各债务值为:d(2,2)=10d(2, 2) = 10,d(3,3)=10d(3, 3) = 10,d(2,3)=5d(2, 3) = 5,d(1,1)=10d(1, 1) = 10,其余所有债务均为 00;
  3. 执行一次第二类操作,其中 a=2a = 2。操作后各债务值为:d(3,3)=10d(3, 3) = 10,d(2,3)=5d(2, 3) = 5,d(1,1)=10d(1, 1) = 10,其余所有债务均为 00;
  4. 执行一次第二类操作,其中 a=3a = 3。操作后各债务值为:d(2,3)=5d(2, 3) = 5,d(1,1)=10d(1, 1) = 10,其余所有债务均为 00;
  5. 执行一次第二类操作,其中 a=1a = 1。操作后各债务值为:d(2,3)=5d(2, 3) = 5,其余所有债务均为 00。

输入解题思路,AI测评打分。不知道怎么写?

首页