CodeForces-376B-I.O.U.

1.题面

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=48700

2.题意

如果A欠B20元,B欠C20元,那么欠款的总值是40元。如果取消B这个中间人,让A欠C20元,那么欠款总值是20元。

求一个等价的欠款网络,让欠款总值最少。

2.思路

我想了很久,但是貌似会很复杂,后来看了一下题解,他说想象有一个很大的银行,谁欠了钱就到那里去还,谁借出去钱就到那里去去。最后每个人的操作金额总值是答案的两倍。

感觉我并没有参透这种思想,所以写一篇日志,留着慢慢参透。

3.代码

/*****************************************************************
    > File Name: tmp.cpp
    > Author: Uncle_Sugar
    > Mail: uncle_sugar@qq.com
    > Created Time: 2016年04月27日 星期三 20时13分24秒
*****************************************************************/
# include <cstdio>
# include <cstring>
# include <cctype>
# include <cmath>
# include <cstdlib>
# include <climits>
# include <iostream>
# include <iomanip>
# include <set>
# include <map>
# include <vector>
# include <stack>
# include <queue>
# include <algorithm>
using namespace std;

const int debug = 1;
const int size  = 5000 + 10; 
const int INF = INT_MAX>>1;
typedef long long ll;

int cnt[size];
int main()
{
	std::ios::sync_with_stdio(false);cin.tie(0);
	int i,j;
	int n,m;
	while (cin >> n >> m){
		memset(cnt,0,sizeof(int)*(n+10));
		while (m--){
			int a,b,c;
			cin >> a >> b >> c;
			cnt[b] += c;
			cnt[a] -= c;
		}
		int sum = 0;
		for (i=1;i<=n;i++){
			sum += abs(cnt[i]);
		}
		cout << sum/2 << endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值