2013
11-11

# Balancing Bank Accounts

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.

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.

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.

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


Case #1
Dagobert Donald 15

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


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

static String[] names = new String[0];
static int[] amount = new int[0];
private static int[][] relation = new int[0][0];

private static int getIndex(String s) {
for (int i = 1; i < names.length; i++) {

if (names[i].equals(s)) {
return i;
}
}

return -1;
}

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

int kase = 0;

while (true) {
String line = stdin.readLine();

String[] temp = line.split("[ ]+");

int n = Integer.parseInt(temp[0]);
int k = Integer.parseInt(temp[1]);

if (n == 0 && k == 0) {
break;
}

kase++;

names = new String[n + 1];

for (int i = 1; i <= n; i++) {
names[i] = stdin.readLine().trim();
}

relation = new int[n + 1][n + 1];
amount = new int[n + 1];

for (int i = 0; i <= n; i++) {
amount[i] = 0;

for (int j = 0; j <= n; j++) {
relation[i][j] = 0;
}
}

for (int i = 1; i <= k; i++) {
line = stdin.readLine();

temp = line.split("[ ]+");

int indexA = getIndex(temp[0]);
int indexB = getIndex(temp[1]);

if (indexA == -1 || indexB == -1) {
//  System.out.println("name==" + temp[0] + "  " + temp[1]);
continue;
}

relation[indexA][indexB] = 1;

int m = Integer.parseInt(temp[2]);
amount[indexA] -= m;

amount[indexB] += m;
}

// firt using foyld wall-shall.
for (int i = 1; i<= n; i++) {
for (int j = 1; j<= n; j++) {
for (int f = 1; f <= n; f++) {
if (relation[i][f] == 1  && relation[f][j] == 1) {
relation[i][j] = 1;
}
}
}
}

System.out.println("Case #" + kase);
for (int i = 1; i <= n; i++) {
if (amount[i] < 0) {
while (amount[i] < 0) {
for (int j = 1; j<= n; j++) {
if (amount[j] <= 0) {
continue;
}
if (relation[i][j] == 1) {
if (amount[j] >= Math.abs(amount[i])) {
System.out.println(names[j] + " " + names[i] + " " + Math.abs(amount[i]));
amount[j] -= Math.abs(amount[i]);
amount[i] = 0;
if (amount[i] == 0) {
break;
}
} else {
System.out.println(names[j] + " " + names[i] + " " + amount[j]);
amount[i] += amount[j];
amount[j] = 0;
break;
}
}
}
}
}
}

System.out.println();
}

}

}