USACO 2.3.5 – Controlling Companies

#include <cstdio>

int main() {
	int N,a,b,p,f=1,g[101][101]={{0}},t[101][101]={{0}},v[101][101]={{0}};
	freopen("concom.in", "r", stdin), freopen("concom.out", "w", stdout);
	scanf("%d", &N);
	for (int i = 0; i < N; g[a][b] = t[a][b] = p, i++)
		scanf("%d %d %d", &a, &b, &p);

	while (f--)
		for (int i = 1; i <= 100; i++)
			for (int j = 1; j <= 100; j++)
				if (t[i][j] >= 50 && !v[i][j]++)
					for (int k = 1; k <= 100; k++)
						if (g[j][k])
							t[i][k] += g[j][k], f=1;

	for (int i = 1; i <= 100; i++)
		for (int j = 1; j <= 100; j++)
			if (i != j && t[i][j] >= 50)
				printf("%d %d\n", i, j);

	return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: