Codeforces Global Round 6 - D. Decreasing Debts(思维)
题意:有$n$个人,$m$个债务关系,$u_{i}$,$v_{i}$,$d_{i}$表示第$u_{i}个人$欠第$v_{i}$个人$d_{i}$块钱,现在你需要简化债务关系,使得债务总额最小。比如,$A$欠$B$十元,$B$欠$C$十五元,$C$欠$A$十元,此时总的债务为$10+15+10=35$,我们可以把债务关系简化为$B$欠$C$五元,那这样总的债务为$5$。
思路:其实每个人只关心自己借出或者借进了多少钱,所以求出每个人借出或者借进了多少钱,再将借进和借出的人分开,两两进行配对,确保每次配对总能消除一个人,比如求出$A$借出$6$元,$B$借进$2$元,$C$借进$4$元,第一次将$A$和$B$配对,消除$B$,此时相当于$A$只借出了$4$元,再将$A$与$C$配对消除$C$即可,因为每次配对都能消除一个人,所以求出来的债务总额也肯定最小。
#include <iostream>
#include <algorithm>
#include <cstdio> using namespace std; typedef long long ll; const int N = ; int n, m, rc, ru[N], rv[N];
int pu[N], pv[N], nu, nv;
ll d[N], rw[N]; int main()
{
scanf("%d%d", &n, &m);
for (int i = ; i <= m; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
d[u] += (ll)w, d[v] -= (ll)w;
}
for (int i = ; i <= n; i++) {
if (d[i] > ) pu[++nu] = i;
if (d[i] < ) pv[++nv] = i;
}
int p1 = , p2 = ;
while (p1 <= nu && p2 <= nv) {
if (d[pu[p1]] > abs(d[pv[p2]])) {
rw[++rc]= abs(d[pv[p2]]);
d[pu[p1]] -= abs(d[pv[p2]]);
ru[rc] = pu[p1], rv[rc] = pv[p2];
p2++;
}
else if (d[pu[p1]] < abs(d[pv[p2]])) {
rw[++rc] = d[pu[p1]];
d[pv[p2]] += d[pu[p1]];
ru[rc] = pu[p1], rv[rc] = pv[p2];
p1++;
}
else {
rw[++rc] = d[pu[p1]];
d[pu[p1]] = d[pv[p2]] = ;
ru[rc] = pu[p1], rv[rc] = pv[p2];
p1++, p2++;
}
}
printf("%d\n", rc);
for (int i = ; i <= rc; i++) printf("%d %d %lld\n", ru[i], rv[i], rw[i]);
return ;
}
Codeforces Global Round 6 - D. Decreasing Debts(思维)的更多相关文章
- Codeforces Global Round 1D(DP,思维)
#include<bits/stdc++.h>using namespace std;int dp[1000007][7][7];int cnt[1000007];int main(){ ...
- Codeforces Global Round 5E(构造,思维)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int main(){ ios::sync_w ...
- Codeforces Global Round 4E(字符串,思维)
#include<bits/stdc++.h>using namespace std;string s,a,b;int main(){ cin>>s; int n=s.size ...
- Codeforces Global Round 12 D. Rating Compression (思维,双指针)
题意:给你一长度为\(n\)的数组,有一长度为\(k\ (1\le k \le n)\)的区间不断从左往右扫过这个数组,总共扫\(n\)次,每次扫的区间长度\(k=i\),在扫的过程中,每次取当前区间 ...
- Codeforces Global Round 9 C. Element Extermination (思维,栈)
题意:有一个长度\(n\)的序列,如果\(a_{i}<a_{i+1}\),那么可以选择删除\(a_{i}\)或者\(a_{i+1}\),再继续操作,问是否能够将序列删到只剩一个元素. 题解:感觉 ...
- CodeForces Global Round 1
CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...
- Codeforces Global Round 1 - D. Jongmah(动态规划)
Problem Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...
- Codeforces Global Round 2 题解
Codeforces Global Round 2 题目链接:https://codeforces.com/contest/1119 A. Ilya and a Colorful Walk 题意: 给 ...
- Codeforces Global Round 1 (A-E题解)
Codeforces Global Round 1 题目链接:https://codeforces.com/contest/1110 A. Parity 题意: 给出{ak},b,k,判断a1*b^( ...
随机推荐
- ffmpeg-- audio decoder
测试代码来源于:http://ffmpeg.org/doxygen/trunk/decode_audio_8c-example.html /* * Copyright (c) 2001 Fabrice ...
- 【转载】Java容器的线程安全
转自:http://blog.csdn.net/huilangeliuxin/article/details/12615507 同步容器类 同步容器类包括Vector和Hashtable(二者是早期J ...
- 378. 有序矩阵中第K小的元素
Q: A: //O(NK) class Solution { public: int kthSmallest(vector<vector<int>>& matrix, ...
- 线性筛-euler,强大O(n)
欧拉函数是少于或等于n的数中与n互质的数的数目 φ(1)=1(定义) 类似与莫比乌斯函数,基于欧拉函数的积性 φ(xy)=φ(x)φ(y) 由唯一分解定理展开显然,得证 精髓在于对于积性的应用: ){ ...
- 关于进程,I/O模型的文章
PHP类 http://rango.swoole.com/archives/508 Node https://github.com/DoubleSpout/threadAndPackage
- 【visio】跨职能流程图
归属于 流程图类别 相比于普通流程图,突出了参与流程的组织.部门之间的联系,形式化地说,它突出的是参与流程的对象之间的联系. 它除了表达基本流程,同时也能展示每个每个流程的归属方,让每个对象明确知道自 ...
- python+matplotlib制作雷达图3例分析和pandas读取csv操作
1.例一 图1 代码1 #第1步:导出模块 import numpy as np import matplotlib.pyplot as plt from matplotlib import font ...
- Ansible - 模块 - shell
概述 ansible 的 shell 模块 准别 ansible 控制节点 ansible 2.8.1 远程节点 OS CentOS 7.5 无密码登录 已经打通 1. 模块 概述 ansible 功 ...
- Django 创建app 应用,数据库配置
一.create project mkdir jango cd jango 目录创建project myapp django-admin startproject myapp 2.在给project创 ...
- Centos7 ISCSI配置 完全攻略
Centos7 ISCSI配置 完全攻略 一. iscsi简单介绍 iSCSI( Internet Small Computer System Interface 互联网小型计算机系统接口) iscs ...