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^( ...
 
随机推荐
- 在网页中插入背景音乐代码(html)
			
有两种 分别用<bgsound>和<embed></embed>标签,当用<embed>插入背景音乐时可以设置宽度和高度为0,隐藏播放器. 二者的参数如 ...
 - gRPC in ASP.NET Core 3.x -- Protocol Buffer(3)更新消息类型
			
当你第一次定义Protocol Buffer的消息的时候,你肯定会给消息设定一套规则需求.但是随着时间的推进,你的业务可能会发生了变化,与此同时,你的Protocol Buffer消息类型的需求也会随 ...
 - kotori和bangdream
			
#include<cstdio> #include<iostream> using namespace std; int n,x,a,b; int main() { cin ...
 - drf的序列化和反序列化
			
序列化器--Serializer 选项参数: max_length 最大长度 min_length 最小长度 allow_blank 是否允许为空 trim_whitespace 是否截断空白字符 m ...
 - [Python] CondaHTTPError: HTTP 000 CONNECTION FAILED for url
			
CondaHTTPError: HTTP 000 CONNECTION FAILED for url 遇到这个问题 解决方法如下两个 一.C:\Users\Administrator 目录下 编辑 . ...
 - Linux - CentOS7 命令行快捷键简介
			
1. 概述 CentOS7 下输入命令的一些快捷键 2. 快捷键 1. 移动 单个字符 ctrl + b/f 单个单词 alt + b/f 行首行尾 ctrl + a/e 2. 编辑 删除单个字符 c ...
 - yii2关闭(开启)csrf的验证
			
(1)全局使用,我们直接在配置文件中设置enableCookieValidation为true request => [ 'enableCookieValidation' => true, ...
 - 后台接口报500,前端获取报错详情message
			
最近和前端对接口的时候,前端说后台接口报500,不能获取里面的东西,然后就开始了这个研究,网上查了一些资料,发现报500里面的报错详情是可以获取的. 前端在调用接口的时候,加个catch,注意!!!这 ...
 - Speech Bandwidth Extension With WaveNet
			
利用WAVENET扩展语音带宽 作者:Archit Gupta, Brendan Shillingford, Yannis Assael, Thomas C. Walters 博客地址:https:/ ...
 - Springmvc-crud-01错误
			
错误:无法显示图书列表内容 原因:获取存储域对象中的名字写错了 controller层: 前端页面: 解决方案:前后端的代码要保持一致(名字自己定义),写代码要细心 修改成功后的界面