#include<iostream> #include<cstdio> #include<queue> usingnamespacestd; constint maxn = 110; int n, m, cc, cnt; int head[maxn], C[maxn], U[maxn], indegree[maxn], outdegree[maxn], topo[maxn]; queue<int> q; structNode { int to, w, next; } G[maxn * maxn];
voidaddedge(int u, int v, int w) { ++cc; G[cc].to = v; G[cc].w = w; G[cc].next = head[u]; head[u] = cc; }
intmain() { scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d %d", &C[i], &U[i]); } for (int i = 1; i <= m; i++) { int u, v, w; scanf("%d %d %d", &u, &v, &w); addedge(u, v, w); indegree[v]++; outdegree[u]++; } for (int i = 1; i <= n; i++) { if (!indegree[i]) { q.push(i); U[i] = 0; } } while (!q.empty()) { int u = q.front(); q.pop(); topo[++cnt] = u; C[u] -= U[u]; for (int i = head[u]; i; i = G[i].next) { int v = G[i].to; int w = G[i].w; indegree[v]--; if (C[u] > 0) { C[v] += w * C[u]; } if (!indegree[v]) q.push(v); } } bool flag = true; for (int i = 1; i <= n; i++) { if (!outdegree[i] && C[i] > 0) { flag = false; printf("%d %d\n", i, C[i]); } } if (flag) { printf("NULL"); } return0; }