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^( ...
随机推荐
- eureka服务只能设置8761,不然服务无法注册
原因是,eureka服务端和eureka的客户端在一个project下,只是属于不同的模块.所以出现了以上问题.分开项目就好了.
- Nginx之server和location配置使用
配置server代码段: server { server_name www.meiduo.site; listen ; root /home/python/Desktop/meiduo_mall_ad ...
- python正则子组匹配
子组匹配返回找到的第一个匹配项 []表示匹配列表中的任意一个,返回找到的第一个 这样可以发现如果要查找字母的话可以使用[a-z],返回找到的第一个字母 查找数字使用[0-9],返回找到的第一个数字相当 ...
- Winform 随机抽奖小程序
效果图: 主要代码: Form1.cs using System; using System.Drawing; using System.IO; using System.Runtime.Intero ...
- 计算几何-Andrew法-凸包
This article is made by Jason-Cow.Welcome to reprint.But please post the article's address. 利用一下叉积和栈 ...
- [lua]紫猫lua教程-命令宝典-L1-01-01. Lua环境与IDE
网上大把的lua教程 不过紫猫老师的教程向来都是讲的非常仔细 所以最近天气已经36+了 魔兽世界还需要冲飞行声望 懒得写单子根本没有单子,正好认认真真的看下紫猫老师的lua教程 紫猫老师的lua教 ...
- mybatis--实现数据库增删改查
首先,创建一个数据库my,并在数据库中插入一张表user,然后在user表中插入一行数据,代码如下: create database my; use my; create table user( id ...
- Linux - Shell - date
概述 date 命令 准备 OS CentOS 7.6 基本功能 显示时间 格式化时间 翻译时间 转换时间格式 切换时区 设置时间 查看文件最后使用时间 1. 显示时间 概述 基本功能 命令 # 内容 ...
- py1
python 下载安装 https://python.org python解释性语言 python数据结构 *输入输出 print(12,34,56,end='',sep='*') input() ...
- 南京邮电大学网络攻防训练平台(NCTF)-异性相吸-Writeup
南京邮电大学网络攻防训练平台(NCTF)-异性相吸-Writeup 题目描述 文件下载地址 很明显,文件之间进行亦或就可得到flag,不再多说,直接上脚本 #coding:utf-8 file_a = ...