https://www.luogu.com.cn/problem/P1038 #include <bits/stdc++.h> #define pii pair<int,int> using namespace std; int n, m; vector<pii> to[10001]; int input_cnt[10001]; int ot[10001]; int C[10001]; int U[10001]; int main(){ scanf("%d%d", &n,&m); for (int i = 1; i <= n;i++){ scanf("%d%d", &C[i], &U[i]); if(C[i]!=0) U[i] = 0; } for (int i = 1; i <= m;i++){ int u, v, w; scanf("%d%d%d", &u, &v, &w); to[u].push_back({w,v}); ot[u] = 1; input_cnt[v]++; } queue<int> que; for (int i = 1; i <= n;i++) if(!input_cnt[i]) que.push(i); while(!que.empty()){ int u = que.front(); que.pop(); for(auto [w,v]:to[u]){ C[v] += w * max((C[u] - U[u]),0); input_cnt[v]--; if(!input_cnt[v]) que.push(v); } } bool flg = 1; for (int i = 1; i <= n;i++){ if(C[i]>U[i]&&!ot[i]){ printf("%d %d\n", i, C[i]-U[i]); flg = 0; } } if(flg) printf("NULL"); }