CodeForces 1266 D. Decreasing Debts

tags: CodeForces  greedy




Topic: Given n n Personal debt relationship, u , v , w u,v,w Just v v Owe u u w w For a dollar, we know that the debt relationship can be transferred, so find all the debt relationships with the least amount of debt.

If we want to minimize the debt relationship at the end, we first count everyone's net debt, then we will save the people who make money, and then use the money of those people who are in debt to pay back the money.

AC code:

#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <stack>
#include <queue>
using namespace std;
#define sd(n) scanf("%d", &n)
#define sdd(n, m) scanf("%d%d", &n, &m)
#define sddd(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define pd(n) printf("%d\n", n)
#define pc(n) printf("%c", n)
#define pdd(n, m) printf("%d %d", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n, m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld", &n)
#define sldd(n, m) scanf("%lld%lld", &n, &m)
#define slddd(n, m, k) scanf("%lld%lld%lld", &n, &m, &k)
#define sf(n) scanf("%lf", &n)
#define sc(n) scanf("%c", &n)
#define sff(n, m) scanf("%lf%lf", &n, &m)
#define sfff(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define ss(str) scanf("%s", str)
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
#define mem(a, n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mod(x) ((x) % MOD)
#define gcd(a, b) __gcd(a, b)
#define lowbit(x) (x & -x)
typedef pair<int, int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
inline int read()
{
    int ret = 0, sgn = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            sgn = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        ret = ret * 10 + ch - '0';
        ch = getchar();
    }
    return ret * sgn;
}
inline void Out(int a) //Êä³öÍâ¹Ò
{
    if (a > 9)
        Out(a / 10);
    putchar(a % 10 + '0');
}

ll gcd(ll a, ll b)
{
    return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b)
{
    return a * b / gcd(a, b);
}
///Fast power m^k%mod
ll qpow(int m, int k, int mod)
{
    ll res = 1, t = m;
    while (k)
    {
        if (k & 1)
            res = res * t % mod;
        t = t * t % mod;
        k >>= 1;
    }
    return res;
}

// Quick power inversion
int Fermat(int a, int p) //Ferma asks for the inverse of a with respect to b
{
    return qpow(a, p - 2, p);
}

///Extended Euclidean
int exgcd(int a, int b, int &x, int &y)
{
    if (b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }
    int g = exgcd(b, a % b, x, y);
    int t = x;
    x = y;
    y = t - a / b * y;
    return g;
}

///Use ecgcd to find the inverse x of a
int mod_reverse(int a, int p)
{
    int d, x, y;
    d = exgcd(a, p, x, y);
    if (d == 1)
        return (x % p + p) % p;
    else
        return -1;
}

///Chinese Remainder Theorem Template
ll china(int a[], int b[], int n) //a[] is the divisor, b[] is the remainder
{
    int M = 1, y, x = 0;
    for (int i = 0; i < n; ++i) //Calculate the result of their multiplication
        M *= a[i];
    for (int i = 0; i < n; ++i)
    {
        int w = M / a[i];
        int tx = 0;
        int t = exgcd(w, a[i], tx, y); //Calculate inverse element
        x = (x + w * (b[i] / t) * x) % M;
    }
    return (x + M) % M;
}
#define pll pair<int, ll>
const int M = 1e5 + 10;
const int N = 2e6 + 10;
struct node
{
    int u, v;
    ll w;
} b[N];
int cnt, pos;
int n, m;
ll a[M];
int u, v;
ll w;
ll sum, res;
queue<pll> q;

int main()
{
    sdd(n, m);
    rep(i, 1, m)
    {
        sdd(u, v);
        sld(w);
        a[u] -= w;
        a[v] += w;
    }
    rep(i, 1, n)
    {
        if (a[i] > 0)
            q.push(make_pair(i, a[i]));//If the person's net debt is to make money
    }
    sum = 0;
    rep(i, 1, n)
    {
        if (a[i] >= 0)
            continue;
        while (a[i])
        {
            pos = q.front().first;
            res = q.front().second;
            q.pop();
            if (res + a[i] > 0)
            {
                res += a[i];
                q.push(make_pair(pos, res));
                b[++cnt].u = i;
                b[cnt].v = pos;
                b[cnt].w = -a[i];
                a[i] = 0;
            }
            else if (res + a[i] == 0)
            {
                b[++cnt].u = i;
                b[cnt].v = pos;
                b[cnt].w = res;
                a[i] = 0;
            }
            else
            {
                a[i] += res;
                b[++cnt].u = i;
                b[cnt].v = pos;
                b[cnt].w = res;
            }
        }
    }
    pd(cnt);
    rep(i, 1, cnt)
        printf("%d %d %lld\n", b[i].u, b[i].v, b[i].w);
    return 0;
}

Intelligent Recommendation

CodeForces - 1353F Decreasing Heights(dp)

Topic link:Click to view The general idea of ​​the topic: Given a matrix of n * m, the weight of each matrix represents the initial height, and now it is necessary to walk from point (0, 0) to point (...

dp-Codeforces-1353F-Decreasing Heights

Topic link Title: An n*m matrix, each grid has a height. To go from (1,1) to (n, m), you can only go up, down, left, and right each time, and you can only go to the grid that is 1 higher than the curr...

CodeForces-1300E Water Balance (decreasing sequence)

🍛 🍛 🍛 The meaning of the question: each time you can choose an interval to distribute them equally, what is the interval with the smallest lexicographical order? It is easy to find that the title r...

CodeForces-1353F Decreasing Heights (DP, thinking)

💦 💦 💦 Title An n*m matrix, each position has a certain height, it is required to go from (1,1) to (n,m) and each movement can only move down or to the right, and it is required to only move towards...

F. Decreasing Heights(Codeforces Round #642 )

Link Meaning Give one n ∗ m n*m n∗mMatrix, the height of the JR column of the first line a i , j a_{i,j} ai,j​Only the high difference of the position of the moving position can only be ...

More Recommendation

AtCoder Beginner Contest 081 D-Non-decreasing [Thinking shift]

D - Non-decreasing Time limit : 2sec / Memory limit : 256MB Score : 600 points Problem Statement Snuke has an integer sequence, a, of length N. The i-th element of a (1-indexed) is ai. He can perform ...

D - Problem D CodeForces - 369A

Valera is a lazy student. He has m clean bowls and k clean plates. Valera has made an eating plan for the next n days. As Valera is lazy, he will eat exactly one dish per day. At that, in order to eat...

Codeforces Round #642 (Div. 3)F-Decreasing Heights(enum+dp)

Title: There is an n * m map, and each location has a height a_ij. Every time you can go down or to the right, and you can only go to the height 1 position. You can modify the height of a certain plac...

Codeforces Round #642 (Div. 3) F. Decreasing Heights (dp)

F. Decreasing Heights Title Given an n*m map, each grid in the map has a height. If you want to walk from [1,1] to [n,m], you can only go down or right, and at the same time, you have to go The height...

codeforces-D- Labyrinth

Topic link To the effect: to a map, the number of times to go right and left is limited, but up and down can go infinitely, asking which points in the figure can pass. Solution: I started writing a si...

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

Top