UVa538 - Balancing Bank Accounts

Balancing Bank Accounts 

Once upon a time there was a large team coming home from the ACM World Finals. The fifteentravellers were confronted with a big problem:


In the previous weeks, there had been many money transactions between them: Sometimessomebody 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 bankaccounts 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 nextyear.

Input 

The input file will contain one or more test cases.

Each test case starts with a line containing two integers: the number of travellers n ($2 \le n \le 20$)and the number of transactions t ($1 \le t \le 1000$). On the next n lines the names of the travellers aregiven, one per line. The names only consist of alphabetic characters and contain no whitespace. Onthe following t lines, the transactions are given in the format name1 name2 amount where name1 isthe person who gave amount dollars to name2 . The amount will always be a non-negative integerless 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 theinput, i.e. balances the accounts again. Use the same format as in the input. Print a blank line aftereach 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
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.io.StreamTokenizer;
import java.util.HashMap;

public class Main 
{
	private static final boolean DEBUG = false;
	private BufferedReader cin;
	private PrintWriter cout;
	private StreamTokenizer tokenizer;
	private int n, t;
	private HashMap<String, Integer> strMap = new HashMap<String, Integer>();
	private HashMap<Integer, String> intMap = new HashMap<Integer, String>();
	private int[] cost;
	private int cas = 1;
	
	public void init() 
	{
		try {
			if (DEBUG) {
				cin = new BufferedReader(new InputStreamReader(
						new FileInputStream("d:\\OJ\\uva_in.txt")));
			} else {
				cin = new BufferedReader(new InputStreamReader(System.in));
			}

			tokenizer = new StreamTokenizer(cin);
			cout = new PrintWriter(new OutputStreamWriter(System.out));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private String next() 
	{
		try {
			tokenizer.nextToken();
			if (tokenizer.ttype == StreamTokenizer.TT_EOF)
				return null;
			else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) {
				return String.valueOf((int) tokenizer.nval);
			} else
				return tokenizer.sval;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public boolean input() 
	{
		n = Integer.parseInt(next());
		t = Integer.parseInt(next());
		
		if (n == 0 && t == 0) return false;
		
		strMap.clear();
		intMap.clear();
		for (int i = 0; i < n; i++) {
			String name = next();
			strMap.put(name, i);
			intMap.put(i, name);
		}
		
		cost = new int[n];
		for (int i = 0; i < t; i++) {
			String name1 = next();
			String name2 = next();
			int amount = Integer.parseInt(next());
			
			int a = strMap.get(name1), b = strMap.get(name2);
			cost[a] -= amount;
			cost[b] += amount;
		}
		return true;
	}

	public void solve() 
	{
		cout.println("Case #" + cas++);
		
		for (int i = 0; i < n; i++) {
			if (cost[i] < 0) {
				for (int j = 0; j < n; j++) {
					if (cost[j] > 0 && cost[i] < 0) {
						int tmp = Math.min(cost[j], -cost[i]);
						cost[i] += tmp;
						cost[j] -= tmp;
						cout.println(intMap.get(j) + " " + intMap.get(i) + " " + tmp);
					}
				}
			}
		}
		cout.println();
		cout.flush();
		
	}

	public static void main(String[] args) 
	{
		Main solver = new Main();
		solver.init();

		while (solver.input()) {
			solver.solve();
		}
	}
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值