#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#define N 1010
#define M 1000000
#define MAXQ 1000000
using namespace std;
const int inf = (-1u >> 1);
int d[N], first[N];
int u[M], v[M] , w[M], next[M];
bool inq[N];
int cnt[N];
int e;
int q[MAXQ];
void addE(int x, int y, int c)
{
next[e] = first[x], first[x] = e;
u[e] = x, v[e] = y;
w[e] = c;
e++;
}
bool SPFA(int s, int n)
{
for (int i = 0; i <= n; i++)d[i] = inf;
memset(cnt, 0, sizeof(cnt));
d[s] = 0;
int qs, qe;
qs = qe = 0;
for (int i = 0; i <= n; i++)inq[i] = false;
q[qe++] = s;
while (qs != qe)
{
int x = q[qs];
inq[x] = false;
for (int e = first[x]; e != -1; e = next[e])if (d[v[e]] > d[u[e]] + w[e])
{
d[v[e]] = d[u[e]] + w[e];
if (!inq[v[e]])
{
inq[v[e]] = true;
q[qe++] = v[e];
if (qe == MAXQ)qe = 0;
if (++cnt[v[e]] >n)return false;
}
}
qs++;
if (qs == MAXQ)qs = 0;
}
return true;
}
int main()
{
int n, m;
// FILE* fp = fopen("in.txt", "r");
while (scanf( "%d", &n) != EOF)
{
scanf( "%d", &m);
int a, b, c;
char tt[3];
e = 0;
for (int i = 0; i <= n; i++)first[i] = -1;
for (int i = 0; i < m; i++)
{
scanf( "%s %d %d", tt, &a, &b);
if (tt[0] == 'V')
addE(a, b, -1);
else {
scanf( "%d", &c);
addE(b, a, c);
addE(a, b, -c);
}
}
for (int i = 1; i <= n; i++)addE(0, i, 0);
if (SPFA(0, n))puts("Reliable");
else puts("Unreliable");
// getchar();getchar();
}
return 0;
}
POJ 2983 Is the Information Reliable?(差分约束#7)
最新推荐文章于 2020-11-06 15:03:24 发布