第12个点wa了,哪里错了呀
查看原帖
第12个点wa了,哪里错了呀
1073916
Kurumi113楼主2024/12/6 01:23
#include <bits/stdc++.h>
using namespace std;
const int N = 2010, M = 51000;
int n, m, idx;
struct {
  int to, w, nxt;
}e[M * 2];

int h[N], dist[N], cnt[N], q[N];
bool st[N];
void add(int a, int b, int c) {
  e[idx] = {b, c, h[a]};
  h[a] = idx++;
}

bool spfa() {
  memset(cnt, 0, sizeof cnt);
  memset(st, false, sizeof st);
  memset(dist, 0x3f, sizeof dist);
  int hh = 0, tt = 0;
  for(int i = 1; i <= n; i++) {
    st[i] = true;
    q[tt++] = i;
  }

  while(hh != tt) {
    int t = q[hh++];
    if(hh == N) hh = 0;
    st[t] = false;
    for(int i = h[t]; ~i; i = e[i].nxt) {
      int j = e[i].to, w = e[i].w;
      if(dist[j] > dist[t] + w) {
        dist[j] = dist[t] + w;
        cnt[j] = cnt[t] + 1;
        if(cnt[j] >= n) return true;
        if(!st[j]) {
          q[tt++] = j;
          if(tt == N) tt = 0;
          st[j] = true;
        }
      }
    }
  }
  return false;
}

int main() {
  int t;
  cin >> t;
  for(; t--; ) {
    scanf("%d%d", &n, &m);
    memset(h, -1, sizeof h);
    for(; m--; ) {
      int a, b, c;
      scanf("%d%d%d", &a, &b, &c);
      if(c < 0) add(a, b, c);
      else add(a, b, c), add(b, a, c);
    }
    if(spfa()) puts("YES");
    else puts("NO");
  }
}

2024/12/6 01:23
加载中...