UVA538 POJ2257 ZOJ1946 Balancing Bank Accounts【序列处理】

509 篇文章 6 订阅
45 篇文章 2 订阅

Balancing Bank Accounts
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1080 Accepted: 509 Special Judge

Description

Once upon a time there was a large team coming home from the ACM World Finals. The fifteen travellers were confronted with a big problem:
In the previous weeks, there had been many money transactions between them: Sometimes somebody paid the entrance fees of a theme park for the others, somebody else paid the hotel room, another one the rental car, and so on.

So now the big calculation started. Some people had paid more than others, thus the individual bank accounts had to be balanced again. “Who has to pay whom how much?”, that was the question.

As such a calculation is a lot of work, we need a program now that will solve this problem next year.

Input

The input will contain one or more test cases.
Each test case starts with a line containing two integers: the number of travellers n (2<=n<=20) and the number of transactions t (1<=t<=1000). On the next n lines the names of the travellers are given, one per line. The names only consist of less than 10 alphabetic characters and contain no whitespace. On the following t lines, the transactions are given in the format name1 name2 amount where name1 is the person who gave amount dollars to name2. The amount will always be a non-negative integer less than 10000.
Input will be terminated by two values of 0 for n and t.

Output

For each test case, first print a line saying “Case #i” where i is the number of the test case.
Then, on the following lines, print a list of transactions that reverses the transactions given in the input, i.e. balances the accounts again. Use the same format as in the input. Print a blank line after each test case, even after the last one.
Additional restrictions:
Your solution must consist of at most n-1 transactions.
Amounts may not be negative, i.e. never output “A B -20”, output “B A 20” instead.

If there is more than one solution satisfying these restrictions, anyone is fine.

Sample Input

2 1
Donald
Dagobert
Donald Dagobert 15
4 4
John
Mary
Cindy
Arnold
John Mary 100
John Cindy 200
Cindy Mary 40
Cindy Arnold 150
0 0

Sample Output

Case #1
Dagobert Donald 15

Case #2
Mary John 140
Cindy John 10
Arnold John 150

Source

Ulm Local 1998

问题链接UVA538 POJ2257 ZOJ1946 Balancing Bank Accounts
问题简述:给定n个人的欠钱关系,求n-1次还清钱方案?
问题分析:每个人都跟第n个人进行清算即可,不论借贷关系。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA538 POJ2257 ZOJ1946 Balancing Bank Accounts */

#include <iostream>
#include <map>
#include <cstring>

using namespace std;

const int N = 20 + 1;
int have[N];
string name[N];

int main()
{
    int k = 0, n, t;
    while (cin >> n >> t && (n || t)) {
        map<string, int> mp;

        for (int i = 1; i <= n; i++) {
            cin >> name[i];
            mp[name[i]] = i;
        }

        memset(have, 0, sizeof have);

        string a, b;
        int w;
        while (t--) {
            cin >> a >> b >> w;
            have[mp[a]] += w;
            have[mp[b]] -= w;
        }

        cout << "Case #" << ++k << endl;
        for (int i = 1; i < n; i++) {
            if (have[i] < 0)
                cout << name[i] << " " << name[n] << " " << -have[i] << endl;
            else if(have[i] > 0)
                cout << name[n] << " " << name[i] << " " << have[i] << endl;
            have[n] -= have[i];
        }
        cout << endl;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值