洛谷3385 【模板】负环
一道负环模板
原题链接
负环模板,不解释。
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 2010;
const int M = 3010;
int fi[N], di[M << 1], ne[M << 1], da[M << 1], dis[N], cnt[N], q[N << 10], l;
bool v[N];
inline int re()
{
int x = 0;
char c = getchar();
bool p = 0;
for (; c<'0' || c>'9'; c = getchar())
p |= c == '-';
for (; c >= '0'&&c <= '9'; c = getchar())
x = x * 10 + (c - '0');
return p ? -x : x;
}
inline void add(int x, int y, int z)
{
di[++l] = y;
da[l] = z;
ne[l] = fi[x];
fi[x] = l;
}
int main()
{
int i, n, m, t, head, tail, x, y, z;
bool p;
t = re();
while (t--)
{
n = re();
m = re();
l = 0;
memset(fi, 0, sizeof(fi));
memset(dis, 60, sizeof(dis));
memset(cnt, 0, sizeof(cnt));
memset(v, 0, sizeof(v));
for (i = 1; i <= m; i++)
{
x = re();
y = re();
z = re();
add(x, y, z);
if (z >= 0)
add(y, x, z);
}
head = 0;
tail = p = 1;
dis[1] = 0;
q[1] = 1;
while (head != tail && p)
{
x = q[++head];
v[x] = 0;
for (i = fi[x]; i; i = ne[i])
{
y = di[i];
if (dis[y] > dis[x] + da[i])
{
dis[y] = dis[x] + da[i];
cnt[y] = cnt[x] + 1;
if (cnt[y] >= n)
{
p = 0;
break;
}
if (!v[y])
{
q[++tail] = y;
v[y] = 1;
}
}
}
}
if (p)
printf("N0\n");
else
printf("YE5\n");
}
return 0;
}
posted on 2018-09-09 19:51 Iowa_Battleship 阅读(121) 评论(0) 编辑 收藏 举报