After the inequality is seen, the model of moving the sleeve
The approximate inequality model is this:\ (x_v <= X_u + w_{(U, v)}\)
\ (X_v-x_u <= w_{(U, v)}\)
From the meiosis to the connected edge
With the shortest-circuited triangular inequalities.
So the solution also revolves around inequalities
It can be seen that the maximum value of \ (x_v\) is a shortest form
If the shortest path does not exist (negative ring), the\ (x_v\) value does not exist
Use DFS_SPFA to judge the shortest way, notice the exit recursion layer when let vis[x] = 0
Also note that the shortest path when initializing the D array is the INF, although the negative ring to initialize to 0 seems to be no problem (also may be the data water), but the initialization of the complexity of the impact is not small ...
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include < queue>using namespace std; #define DEBUG (x) cerr << #x << "=" << x << endl;const int MAXN = 1000 0 + 10;int n,m,vis[maxn],cnt[maxn],last[maxn],edge_tot,d[maxn];struct edge{int u, V, W, to; Edge () {} edge (int u, int v, int w, int to): U (U), V (v), W (W), to (to) {}}E[MAXN * 2];inline void Add (int u, int v, int W) {E[++edge_tot] = Edge (U, V, W, Last[u]); Last[u] = Edge_tot; }bool flg;queue<int> q;void SPFA (int x) {vis[x] = 1; if (FLG) return; for (int i=last[x]; i; i=e[i].to) {int v = e[i].v, w = E[I].W; if (D[v] > D[x] + W) {if (Vis[v]) {FLG = true;//is accessed again in the recursive layer, and the path is shorter, is it possible to walk this way more than once? Return } D[v] = d[x] + W; SPFA (v); }} Vis[x] = 0;} int main () {scanf ("%d%d", &n, &m); for (int i=1; i<=m; i++) {iNT CMD, A, B, C; scanf ("%d", &cmd); if (cmd = = 1) {scanf ("%d%d%d", &a, &b, &c); Add (A, B,-C); } else if (cmd = = 2) {scanf ("%d%d%d", &a, &b, &c); Add (b, A, c); } else {scanf ("%d%d", &a, &b); Add (A, b, 0); Add (b, a, 0); }} memset (d, 0x3f, sizeof (d)); for (int i=1; i<=n; i++) {d[i] = 0; SPFA (i); if (FLG) break; } if (FLG) printf ("No"); else printf ("Yes"); return 0;}
In addition, the BFS award negative ring (the upper bound of Complexity is n*m):
bool SPFA(int s) { dist[s] = 0; q.push(s); vis[s] = 1; cnt[s] = 0; while(!q.empty()) { int x = q.front(); q.pop(); vis[x] = 0; for(int i=last[x]; i; i=e[i].to) { int v = e[i].v; int w = e[i].w; if(dist[v] > dist[x] + w) { dist[v] = dist[x] + w; cnt[v] = cnt[x] + 1; if(cnt[v] >= n) { return 1; } if(!vis[v]) q.push(v), vis[v] = 1; } } } return 0;}
Farm of P1993 small K-differential constraints