It took a long time. The difference constraint is finally written. If the food cannot afford to hurt ~~~ Difference constraints: 1. Find the constraints. 2. Create a graph. 3. Use spfa to find constraints. You need to understand the meaning of the question and pay more attention to hidden conditions. You can understand each edge at least when creating a graph. [Html] # include <stdio. h> # include <string. h >#include <algorithm> # include <iostream> # include <queue> # define INF 999999; using namespace std; struct list {int v; int value; int next ;}; struct list node [151000]; int head [100000]; int num = 1; int maxx, minn; void add (int a, int B, int c) {node [num]. v = B; node [num]. value = c; node [num]. next = head [a]; head [a] = num; num ++;} int spfa () {int dist [100000], I; que Ue <int> q; for (I = minn; I <= maxx; I ++) {dist [I] =-INF;} q. push (minn); dist [minn] = 0; while (! Q. empty () {int e; e = q. front (); q. pop (); for (I = head [e]; I = node [I]. next) {if (dist [node [I]. v] <dist [e] + node [I]. value) {dist [node [I]. v] = dist [e] + node [I]. value; q. push (node [I]. v) ;}}return dist [maxx];} int main () {int n, I, a, B, c; while (~ Scanf ("% d", & n) {memset (head, 0, sizeof (head); maxx =-INF; minn = INF; num = 1; for (I = 0; I <n; I ++) {cin> a> B> c; if (maxx <B + 1) maxx = B + 1; if (minn> a) minn = a; add (a, B + 1, c);} www.2cto.com for (I = minn; I <maxx; I ++) {add (I, I + 1, 0); add (I + 1, I,-1);} printf ("% d \ n", spfa ());} return 0 ;}