#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
inline int read()
{
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')
f = -f;
ch = getchar();
}
while (isdigit(ch))
{
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return f * x;
}
#define N 110
#define M 10010
int n, m;
struct Edge
{
int to, next, val;
} e[M];
int head[M];
int cnt;
inline void add(int from, int to, int val)
{
e[++cnt].to = to;
e[cnt].val = val;
e[cnt].next = head[from];
head[from] = cnt;
}
int c[N], u[N];
int in[N];
int is_out[N];
queue<int> q;
int vis[N];
int main()
{
n = read(), m = read();
for (int i = 1; i <= n; ++i)
{
c[i] = read(), u[i] = read();
if (c[i] > 0)
c[i] += u[i];
is_out[i] = true;
}
int t1, t2, t3;
for (int i = 1; i <= m; ++i)
{
t1 = read(), t2 = read(), t3 = read();
add(t1, t2, t3);
is_out[t1] = false;
++in[t2];
}
for (int i = 1; i <= n; ++i)
if (!in[i])
{
q.push(i);
vis[i] = true;
}
while (!q.empty())
{
int now = q.front();
q.pop();
c[now] -= u[now];
if (c[now] > 0)
for (int i = head[now]; i; i = e[i].next)
{
int to = e[i].to;
c[to] += c[now] * e[i].val;
in[to]--;
if (!in[to] && !vis[to])
{
q.push(to);
vis[to] = true;
}
}
}
int book = 0;
for (int i = 1; i <= n; ++i)
if (is_out[i] && c[i] > 0)
{
book = 1;
printf("%d %d\n", i, c[i]);
}
if (!book)
{
printf("NULL\n");
}
return 0;
}
其中的
for (int i = 1; i <= n; ++i)
{
c[i] = read(), u[i] = read();
if (c[i] > 0)
c[i] += u[i];
is_out[i] = true;
}
c[i] > 0 我因样例先写成 c[i] == 1,仍可通过前四个测试点。