Sub-prime
The most recent economic crisis was partly caused by the manner in which banks made loans to people unable to repay them and resold such loans to other banks as debentures. Obviously, when people failed to repay their loans, the whole system collapsed.
Sub-prime |
The crisis was so deep that it affected countries all over the world, including Nlogonia, where the honored prime minister Man Dashuva ordered the Central Bank chairman to come up with a solution. He came up with a brilliant idea: if all banks could liquidate its debentures only with its own monetary reserves, all banks would survive and the crisis would be averted.
However, with the elevated number of debentures and banks involved, this was an extremely complicated task, so he asked for your help in writing a program that, given the banks and the debentures printed by them, determines if it is possible that all banks pay back their debts using only their monetary reserves and credits.
Input
The input consists of several test cases. The first line of each test case contains two integers B and N, indicating the number of banks (1














The end of input is indicated by a line containing only two zeros, separated by spaces.
Output
For each test case, your program should print a single line, containing a single character: `S', if it is possible to liquidate all debentures without a bailout from the Central Bank of Nlogonia, or `N' if a bailout is necessary.Sample Input
3 3 1 1 1 1 2 1 2 3 2 3 1 3 3 3 1 1 1 1 2 1 2 3 2 3 1 4 3 3 1 1 1 1 2 2 2 3 2 3 1 2 0 0
Sample Output
S N S计算从debtor到creditor,最后如果每个银行的余值大于等于0,输出s
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 21;
int b, n;
int cost[N];
bool input();
void solve();
int main()
{
#ifndef ONLINE_JUDGE
freopen("d:\\OJ\\uva_in.txt", "r", stdin);
#endif
while (input()) {
solve();
}
return 0;
}
bool input()
{
scanf("%d%d", &b, &n);
if (b == 0 && n == 0) return false;
memset(cost, 0x00, sizeof(cost));
for (int i = 1; i <= b; i++) {
scanf("%d", &cost[i]);
}
for (int i = 0; i < n; i++) {
int debtor, creditor, debenture;
scanf("%d%d%d", &debtor, &creditor, &debenture);
cost[debtor] -= debenture;
cost[creditor] += debenture;
}
return true;
}
void solve()
{
bool ok = true;
for (int i = 1; i <= b; i++) {
if (cost[i] < 0) {
ok = false;
break;
}
}
printf("%s\n", ok ? "S" : "N");
}