D. Decreasing Debts----Thinking

tags: Codeforces  thinking

There are n people in this world, conveniently numbered 1 through n. 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) denote the debt of a towards b, or 0 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:

Let d(a,b)>0 and d(c,d)>0 such that a≠c or b≠d. We can decrease the d(a,b) and d(c,d) by z and increase d(c,b) and d(a,d) by z, where 0<z≤min(d(a,b),d(c,d)).
Let d(a,a)>0. We can set d(a,a) to 0.
The total debt is defined as the sum of all debts:

Σd=∑a,bd(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.

Input
The first line contains two space separated integers n (1≤n≤105) and m (0≤m≤3⋅105), representing the number of people and the number of debts, respectively.

m lines follow, each of which contains three space separated integers ui, vi (1≤ui,vi≤n,ui≠vi), di (1≤di≤109), meaning that the person ui borrowed di burles from person vi.

Output
On the first line print an integer m′ (0≤m′≤3⋅105), 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′ lines, i-th of which contains three space separated integers ui,vi,di, meaning that the person ui owes the person vi exactly di burles. The output must satisfy 1≤ui,vi≤n, ui≠vi and 0<di≤1018.

For each pair i≠j, it should hold that ui≠uj or vi≠vj. In other words, each pair of people can be included at most once in the output.

Examples
inputCopy

3 2
1 2 10
2 3 5
outputCopy
2
1 2 5
1 3 5
inputCopy
3 3
1 2 10
2 3 15
3 1 10
outputCopy
1
2 3 5
inputCopy
4 2
1 2 12
3 4 8
outputCopy
2
1 2 12
3 4 8
inputCopy
3 4
2 3 1
2 3 2
2 3 4
2 3 8
outputCopy
1
2 3 15
Note
In the first example the optimal sequence of operations can be the following:

Perform an operation of the first type with a=1, b=2, c=2, d=3 and z=5. The resulting debts are: d(1,2)=5, d(2,2)=5, d(1,3)=5, all other debts are 0;
Perform an operation of the second type with a=2. The resulting debts are: d(1,2)=5, d(1,3)=5, all other debts are 0.
In the second example the optimal sequence of operations can be the following:

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

Title: There are n people and m debt relationships. u v d means that u owes v d yuan money, the debt relationship can be transferred, and find all the debt relationships with the least amount of debt.

Analysis: We only consider how much we spend and how much we earn. Then match each other to offset each other's own expenses and income (see the code in detail). If the offset is 0, then change to the next person.



#include<bits/stdc++.h>
using namespace std;
const int N=3e5+1000;
typedef long long ll;
ll v[N];
vector<int> in,out,ans1,ans2;
vector<ll> ans3;
int n,m; 
int main()
{
	scanf("%d %d",&n,&m);
	while(m--)
	{
		int x,y;ll z;
		cin>>x>>y>>z;
		v[x]+=z; //expenditure
		v[y]-=z;//Income 
	}
	for(int i=1;i<=n;i++)
	{
		if(v[i]>0) out.push_back(i); //Expenditure 
		if(v[i]<0) in.push_back(i);//Income 
	 } 
	 int i=0,j=0;
	 while(1)
	 {
	 	if(i==out.size()||j==in.size()) break;
	 	ll value=min(v[out[i]],-v[in[j]]);
	 	ans1.push_back(out[i]);
	 	ans2.push_back(in[j]);
	 	ans3.push_back(value);
	 	v[out[i]]-=value;
	 	v[in[j]]+=value;
	 	if(v[out[i]]==0) i++; //Change to the next person to match
	 	if(v[in[j]]==0) j++;
	 	
	  }
	  cout<<ans1.size()<<endl;
	  for(int i=0;i<ans1.size();i++)
	  {
	  	cout<<ans1[i]<<" "<<ans2[i]<<" "<<ans3[i]<<endl;
	   } 
	   return 0; 
}

Intelligent Recommendation

Brand 665. Non-decreasing number of thinking

https://leetcode-cn.com/problems/non-decreasing-array/submissions/ Idea: Although it is a simple question, it is still very tangible. Don't wish to have a i > a i + 1 a_i>a_{i+1} ai​>ai+1​, T...

D. Inversion Counting (thinking thinking)

The thinking is still not sharp enough... Seeing this kind of data range and only need to output parity, it should be very simple each Times turn turn Area between , change change of only Yes Area bet...

D. Gourmet choice (thinking)

Original title: http://codeforces.com/contest/1131/problem/D Intention: There is an n-length array a, an array of length b, giving an n*m matrix x, and x[i][j] represents the relationship between a[i]...

D. The Beatles (Thinking + Enumeration)

D. The Beatles   Intention: There are n*k cities, the distance between each city is 1km, and there are restaurants in the 1st, 1+k, 1+k*2..., 1+(n-1)*k cities. Starting from the city s, the dista...

D - Grid Coloring (thinking)

Topic link:https://atcoder.jp/contests/arc080/tasks/arc080_b Problem Statement We have a grid with HH rows and WW columns of squares. Snuke is painting these squares in colors ...

More Recommendation

D - Crazy Penguin - thinking

D - Crazy Penguin  CSU - 2147  Meaning of the questions: Every morning, if a small penguin penguins small numbers found on his right than he is small, he will destroy the little penguin. Ask...

CF851 D enumeration thinking

Given number n, you can put it into each number 0, 1 or increase, respectively, takes x, y. Ask all the GCD minimum number becomes not spend 1 it is. n ranges 5x1e5, a [i] in the range 1e6. Want to st...

D. Sea Battle [thinking]

D. Sea Battle Meaning of the title: a string s, to the empty place to put a long boat to b, of course, a number can not intersect. s [i] == 1 represents the point of this ban a vessel, at least ask a ...

【Thinking】 D. Sequence Sorting

D. Sequence Sorting time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a sequence a1,a2,…,ana1,a2,…,an, cons...

D. Nastya and a Game【Thinking】

Nastya and a Game Topic: Given an Arr[n], find the number of intervals that satisfy multipul[L,R]/sum[L,R]==k Idea: For the case of a[i]==1, because it has no effect on multipul, it only affects L and...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top