WA on #3 求条
查看原帖
WA on #3 求条
1378591
Barewalk楼主2025/1/8 20:15

rt。

#include <iostream>

#define N 105

using namespace std;

int nxt[N], h[N], w[N], t[N], cnt;
void add(int x, int y, int v) {
    nxt[++cnt] = h[x], h[x] = cnt, t[cnt] = y, w[cnt] = v;
}

int ind[N], oud[N], q[N], st = 1, ed;

int n, p, c[N], u[N];

int main() {
    cin >> n >> p;
    for (int i = 1; i <= n; i++) cin >> c[i] >> u[i];
    if (!p) return cout << "1 " << c[1] << "\n", 0;
    for (int i = 1; i <= p; i++) {
        int x, y, v; cin >> x >> y >> v;
        add(x, y, v); ind[y]++, oud[x]++;
    }
    for (int i = 1; i <= n; i++)
        if (!ind[i]) q[++ed] = i;
    while (st <= ed) {
        int x = q[st++];
        c[x] -= u[x];
        if (c[x] <= 0) continue;
        for (int i = h[x]; i; i = nxt[i]) {
            int v = t[i]; ind[v]--;
            if (!ind[v]) q[++ed] = v;
            c[v] += w[i] * c[x];
        }
    }
    bool Null = 1;
    for (int i = 1; i <= n; i++)
        if (!oud[i] && c[i] > 0) {
            Null = 0;
            cout << i << " " << c[i] << "\n";
        }
    if (Null) puts("NULL");
    return 0;
}
2025/1/8 20:15
加载中...