UVa11679 - Sub-prime

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.

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$ \le$B$ \le$20) and the number of debentures printed by the banks (1$ \le$N$ \le$20). The banks are identified by integers between 1 and B. The second line contains B integers Ri separated by spaces, indicating the monetary reserves of each one of the B banks (0$ \le$Ri$ \le$104, for 1$ \le$i$ \le$B). The N following lines contain each one three integers separated by spaces: an integer D, indicating the debtor bank (1$ \le$D$ \le$B), an integer C , indicating the creditor bank (1$ \le$C$ \le$B and D$ \ne$C) and an integer V, indicating the debenture value (1$ \le$V$ \le$104).

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");
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

kgduu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值