题目链接:
http://www.lydsy.com/JudgeOnline/problem.php?id=3436
题解:
裸的差分约束:
1、a>=b+c -> b<=a-c -> d[v]<=d[u]+w -> 建一条边从a到b,权值为-c
2、a<=b+c -> d[v]<=d[u]+w -> 建一条边从b到a,权值为c
3、a==b -> d[v]<=d[u]+0&&d[u]<=d[v]+0 建一条边从a到b,权值为0。建一条边从b到a,权值为0.
建完图,从原点0到各点连边,权值都为0,跑最短路。
代码:
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<queue>
#define mp make_pair
#define X first
#define Y second
using namespace std; const int maxn = + ; vector<pair<int,int> > G[maxn]; int n, m; int inq[maxn], d[maxn], cnt[maxn];
bool spfa(int s) {
memset(inq, , sizeof(inq));
memset(d, 0x7f, sizeof(d));
memset(cnt, , sizeof(cnt));
queue<int> Q;
d[s] = , inq[s] = , Q.push(s);
while (!Q.empty()) {
int u = Q.front(); Q.pop();
inq[u] = ;
for (int i = ; i < G[u].size(); i++) {
int v = G[u][i].X,w=G[u][i].Y;
if (d[v] > d[u] + w) {
d[v] = d[u] + w;
if (!inq[v]) {
inq[v] = , Q.push(v);
if (++cnt[v] > n + ) return false;
}
}
}
}
return true;
} void init() {
for (int i = ; i <= n; i++) G[i].clear();
} int main() {
while (scanf("%d%d", &n, &m) == && n) {
init();
for (int i = ; i <= n; i++) {
G[].push_back(mp(i, ));
}
for (int i = ; i < m; i++) {
int cmd, a, b,c;
scanf("%d", &cmd);
if (cmd == ) {
scanf("%d%d%d", &a, &b, &c);
G[a].push_back(mp(b, -c));
}
else if (cmd == ) {
scanf("%d%d%d", &a, &b, &c);
G[b].push_back(mp(a, c));
}
else {
scanf("%d%d", &a, &b);
G[a].push_back(mp(b, ));
G[b].push_back(mp(a, ));
}
}
bool ans = spfa();
if (ans) puts("Yes");
else puts("No");
} return ;
} /*
3 3
3 1 2
1 1 3 1
2 2 3 2
*/