zoukankan      html  css  js  c++  java
  • 【cf1266D】D. Decreasing Debts(贪心)

    传送门

    题意:
    现有(n)个人,有(m)对欠债关系:(d(a,b))表示(a)(b d(a,b))元。
    现要给出一个最终的欠债关系,使得(displaystyle sum d)最小。

    思路:
    一开始以为这是图论上的问题,需要什么奇技淫巧使得最后图中没有无用的边。
    实际上我们只需要考虑终态,即每个人需要给出多少钱,收到多少钱,根据这个直接建图即可。
    代码如下:

    /*
     * Author:  heyuhhh
     * Created Time:  2020/3/7 15:04:42
     */
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <vector>
    #include <cmath>
    #include <set>
    #include <map>
    #include <queue>
    #include <iomanip>
    #include <assert.h>
    #define MP make_pair
    #define fi first
    #define se second
    #define pb push_back
    #define sz(x) (int)(x).size()
    #define all(x) (x).begin(), (x).end()
    #define INF 0x3f3f3f3f
    #define Local
    #ifdef Local
      #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
      void err() { std::cout << '
    '; }
      template<typename T, typename...Args>
      void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
      template <template<typename...> class T, typename t, typename... A> 
      void err(const T <t> &arg, const A&... args) {
      for (auto &v : arg) std::cout << v << ' '; err(args...); }
    #else
      #define dbg(...)
    #endif
    using namespace std;
    typedef long long ll;
    typedef pair<int, int> pii;
    //head
    const int N = 1e5 + 5, M = 3e5 + 5;
    
    int n, m;
    ll d[N];
    vector <int> pos, neg;
    
    void run() {
        cin >> n >> m;
        for(int i = 1; i <= m; i++) {
            int u, v, w; cin >> u >> v >> w;   
            d[u] += w, d[v] -= w;
        }
        for(int i = 1; i <= n; i++) {
            if(d[i] > 0) pos.push_back(i);
            if(d[i] < 0) neg.push_back(i);
        }
        vector <pair<pii, ll>> ans;
        while(!pos.empty()) {
            int i = pos.back(), j = neg.back();
            pos.pop_back(), neg.pop_back();
            ll t = min(d[i], -d[j]);
            d[i] -= t, d[j] += t;
            if(d[i] > 0) pos.push_back(i);
            if(d[j] < 0) neg.push_back(j);
            ans.push_back(MP(MP(i, j), t));
        }
        cout << sz(ans) << '
    ';
        for(auto it : ans) {
            cout << it.fi.fi << ' ' << it.fi.se << ' ' << it.se << '
    ';   
        }
    }
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(0); cout.tie(0);
        cout << fixed << setprecision(20);
        run();
        return 0;
    }
    
  • 相关阅读:
    学习Vue.js
    Xmind思维导图
    Webpack 入门教程
    Vscode 使用介绍
    jdk,jre下载安装
    Java JDK下载
    FileZilla FTP下载
    notepad++下载
    windows环境搭建Vue开发环境
    SecureCRTPortable的安装和使用(图文详解)
  • 原文地址:https://www.cnblogs.com/heyuhhh/p/12451459.html
Copyright © 2011-2022 走看看