Question:
There is a sequence in which the question is described by a combination of N integers [ai, Bi, CI]. [ai, Bi, CI] indicates that the sequence is in [ai, BI] The integers in this interval must have at least one CI. If such a sequence exists, the minimum length of the sequence that meets the requirements of the question is requested. If not,-1 is output.
Input: the first line contains an integer N, indicating the number of intervals. Each row in the N rows below describes these intervals. The three integers AI, Bi, and Ci in the I + 1 are separated by spaces, 0 <= AI <= Bi <= 50000 and 1 <= CI <= Bi-ai + 1.
Output: a row that outputs the minimum length of a sequence that meets the requirements.
Sum [I] = Sigma [1, I]
Inequality:
Sum [B [I]-sum [A [I]-1]> = Ci
Sum [I + 1]-sum [I]> = 0
Sum [I]-sum [I + 1]> =-1
For
D [v]> = d [u] + W, which can be converted to the longest path (u-> V edge weight is W) to obtain the minimum value of d [I ].
/* Sum (I) = Sigma [1 .. i] * sum (BI)-sum (AI)> = CI * sum (I)-sum (I-1)> = 0 * sum (I-1)-sum (I)> =-1 * d (v)> = d (u) + W, find the longest path **/# include <queue> # include <cstdio> # include <cstring> # include <iostream> # include <algorithm> using namespace STD; const int maxn = 50000 + 100; const int maxm = 500000 + 100; const int INF = 1e8; int head [maxn], TOT; struct edge {int to, W, next;} edge [maxm]; void Init () {tot = 0; memset (Head,-1, sizeof head);} void addedge (int u, int V, int W) {edge [tot]. to = V; edge [tot]. W = W; edge [tot]. next = head [u]; head [u] = tot ++;} int d [maxn]; bool vis [maxn]; queue <int> q; int Mn, MX; int spfa () {int S = Mn; while (! Q. empty () Q. pop (); For (INT I = Mn; I <= Mx; ++ I) d [I] =-INF; memset (VIS, false, sizeof vis); q. push (s); D [s] = 0; vis [s] = true; while (! Q. Empty () {int u = Q. Front (); q. Pop (); vis [u] = false; For (INT I = head [u]; ~ I; I = edge [I]. next) {Int & V = edge [I]. to; Int & W = edge [I]. w; If (d [v] <D [u] + W) {d [v] = d [u] + W; If (! Vis [v]) {vis [v] = true; q. push (v) ;}}} return d [MX];} int main () {int N, U, V, W; Init (); scanf ("% d", & N); Mn = inf, MX =-INF; For (INT I = 0; I <n; ++ I) {scanf ("% d", & U, & V, & W); addedge (u, v + 1, W); Mn = min (Mn, u); MX = max (MX, V + 1);} For (INT I = Mn; I <MX; ++ I) {addedge (I, I + 1, 0); addedge (I + 1, I,-1);} printf ("% d \ n", spfa (); Return 0 ;}
Poj1201 intervals, difference constraint, spfa