40分求助
查看原帖
40分求助
768951
_Chronostatis_楼主2024/7/16 20:06
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1e2 + 10;

struct Node {
  int u, c;
} a[MAXN];

int n, m, ind[MAXN], outd[MAXN], g2[MAXN][MAXN];
bool vis[MAXN], v2[MAXN];
vector<int> g[MAXN];
queue<int> q;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i].c >> a[i].u;
  }
  for (int i = 1, u, v, w; i <= m; i++) {
    cin >> u >> v >> w;
    g[u].push_back(v);
    g2[u][v] = w;
    ind[v]++;
    outd[u]++;
  }
  for (int i = 1; i <= n; i++) {
    if (!ind[i]) {
      q.push(i);
      v2[i] = 1;
    }
  }
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    if (vis[x]) {
      continue;
    }
    vis[x] = 1;
    for (int y : g[x]) {
      a[y].c += g2[x][y] * a[x].c;
      ind[y]--;
      if (!ind[y]) {
        q.push(y);
      }
    }
    if (!v2[x]) {
      a[x].c -= a[x].u;
    }
  }
  int f = 0;
  for (int i = 1; i <= n; i++) {
    if (!outd[i] && a[i].c > 0) {
      cout << i << ' ' << a[i].c << '\n';
      f = 1;
    }
  }
  if (!f) {
    cout << "NULL";
  }
  return 0;
}

#1,2,3 WA

2024/7/16 20:06
加载中...