Poj-1201-Intervals-difference Constraints

Source: Internet
Author: User

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 ;}

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.