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;
}