为什么我的代码里面N开1005会WA两个点,开5005才AC,话说数据范围不是 n≤1000 吗?
#include <iostream>
#include <cstring>
using namespace std;
const int N = 5005, M = 3 * 5005 + 1005, INF = 0x3f3f3f3f;
int n, m, idx;
int h[N], e[M], w[M], ne[M];
int dist[N], q[N], cnt[N];
bool st[N];
void add(int a, int b, int c)
{
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx ++ ;
return;
}
bool spfa()
{
memset(dist, -0x3f, sizeof dist);
memset(st, 0, sizeof st);
memset(cnt, 0, sizeof cnt);
int hh = 0, tt = 1;
q[0] = 0;
st[0] = 1;
dist[0] = 0;
while (hh != tt)
{
int t = q[hh ++ ];
if (hh == N) hh = 0;
st[t] = 0;
for (int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
if (dist[j] < dist[t] + w[i])
{
dist[j] = dist[t] + w[i];
cnt[j] = cnt[t] + 1;
if (cnt[j] >= n + 1) return 0;
if (!st[j])
{
q[tt ++ ] = j;
if (tt == N) tt = 0;
st[j] = 0;
}
}
}
}
return 1;
}
int main()
{
cin >> n >> m;
memset(h, -1, sizeof h);
while (m -- )
{
int a, b, c;
cin >> a >> b >> c;
add(a, b, -c); // longest way
add(0, a, 0);
add(0, b, 0);
}
for (int i = 1; i <= n; i ++ ) add(0, i, 0);
if (!spfa()) cout << "NO SOLUTION";
else
{
for (int i = 1; i <= n; i ++ ) cout << dist[i] << endl;
}
return 0;
}