Poj1201 intervals, difference constraint, spfa

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.