题目就是给你一组由等式和不等式组成的式子,让你判断这组式子是否有解。为了使用差分约束系统,将等式a-b=c转化为a-b>=c和a-b<=c。然后就是构图判断是否有负环就行了。
代码如下:
#include <cstdio>
#include <stack>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <iomanip>
#include <cmath>
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(arr, num) memset(arr, num, sizeof(arr))
#define PB push_back
#define F first
#define S second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid + 1, r
#define LRT rt << 1
#define RRT rt << 1|1
#define BitCount(x) __builtin_popcount(x)
#define BitCountll(x) __builtin_popcountll(x)
#define LeftPos(x) 32 - __builtin_clz(x) - 1
#define LeftPosll(x) 64 - __builtin_clzll(x) - 1
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
using namespace std;
const double eps = 1e-8;
const int MAXN = 300 + 10;
const int MOD = 1000007;
const int M=20010;
const int N=300010;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
typedef pair<int, int> pii;
int n,m,r,d[N];
int w[N],u[N],v[N],first[N],next[N];
bool vis[N];
bool bellman()
{
int i,j;
for (i=1;i<=n;d[i++]=INF);
d[1]=0;
bool flag=false;
for (i=0;i<n;i++){
flag=false;
for (j=0;j<m;j++){
if (d[v[j]]>d[u[j]]+w[j]){
d[v[j]]=d[u[j]]+w[j];
flag=true;
}
}
if (!flag) return true; // 在一次循环里对所有边都没有更新,说明没有负环
}
if (flag) return false; // 有负环
else return true; // 没有负环
}
int main()
{
int i,j;
while(~scanf("%d%d",&n,&m))
{
char s[N];
int a,b,c;
for (i=0,j=0;i<m;i++){
scanf("%s",s);
if (s[0]=='P') {
scanf("%d%d%d",&a,&b,&c);
u[j]=a; v[j]=b; w[j++]=c;
u[j]=b; v[j]=a; w[j++]=-c;
}
else {
scanf("%d%d",&a,&b);
u[j]=b; v[j]=a; w[j++]=-1;
}
}
m=j;
if (bellman()) puts("Reliable");
else puts("Unreliable");
}
}