CF376B.I.O.U.
普及/提高-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Imagine that there is a group of three friends: A, B and С. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles.
This task is a generalisation of a described example. Imagine that your group of friends has n people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem.
假设有一组三位朋友:A、B 和 C。A 欠 B 20 卢布,B 欠 C 20 卢布。债务总额为 40 卢布。可以看出,这些债务的组织方式并非最优。我们重新安排如下:假设 A 欠 C 20 卢布,而 B 不欠任何人任何钱。此时债务所表达的经济关系完全不变,但债务总额降为 20 卢布。
本题是对上述例子的推广。假设你的朋友群体共有 n 人,且你知道他们之间所有的债务关系。请在不改变债务实际含义的前提下,对债务进行优化。换言之,最终每位朋友“应支付的总金额”与“应收取的总金额”之间的差值必须保持不变。请输出在最优债务重组方案下,所有债务金额的最小总和。参见样例测试的注释以更深入理解本题。
输入格式
The first line contains two integers n and m (1 ≤ n ≤ 100; 0 ≤ m ≤ 104). The next m lines contain the debts. The i-th line contains three integers a__i, b__i, c__i (1 ≤ a__i, b__i ≤ n; a__i ≠ b__i; 1 ≤ c__i ≤ 100), which mean that person a__i owes person b__i c__i rubles.
Assume that the people are numbered by integers from 1 to n.
It is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (x, y) and pair of people (y, x).
第一行包含两个整数 n 和 m(1≤n≤100;0≤m≤104)。接下来的 m 行描述债务关系。第 i 行包含三个整数 ai,bi,ci(1≤ai,bi≤n;ai=bi;1≤ci≤100),表示第 ai 个人欠第 bi 个人 ci 卢布。
假设所有人按整数编号,编号范围为 1 到 n。
保证输入中同一对人至多出现一次。输入中不会同时包含人对 (x,y) 和人对 (y,x)。
输出格式
Print a single integer — the minimum sum of debts in the optimal rearrangement.
输出一个整数——最优重新安排下的最小债务总和。
输入输出样例
输入#1
5 3 1 2 10 2 3 1 2 4 1
输出#1
10
输入#2
3 0
输出#2
0
输入#3
4 3 1 2 1 2 3 1 3 1 1
输出#3
0
说明/提示
In the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10.
In the second sample, there are no debts.
In the third sample, you can annul all the debts.
在第一个样例中,可以假设编号为 1 的人欠编号为 2 的人 8 卢布、欠编号为 3 的人 1 卢布、欠编号为 4 的人 1 卢布,且不欠其他人任何款项。最终,总债务为 10。
在第二个样例中,不存在任何债务。
在第三个样例中,所有债务均可相互抵消。
输入解题思路,AI测评打分。不知道怎么写?