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 ()and the number of transactions t (). 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();
}
}
}