Poj 3159 candies difference Constraint

Source: Internet
Author: User

Set a [I] to the number of sweets for the I-th child, and D [I] to the number of sweets for the I-th child relative to the 1st child, that is:

D [I] = A [I]-A [1], d [1] = 0.

A B c includes a [B]-A [a] <= C (A [B]-A [1]). -(A [a]-A [1]) <= c, d [B]-d [a] <= C

The question is to find the maximum value, that is, to find the shortest path with 1 as the source point.

Note: The use of spfa + queues will be Tlm. It takes 700 ms to switch to stack. I tried Dijkstra + priority_queue and wiped it over 1400 + Ms. I wonder if it is not well written.

 

# Include <iostream> # include <queue> # include <stack> using namespace STD; const int max = 30005; const int INF = 1000000000; const int n = 150005; struct node {int V; int cost; int next ;}; node [N]; int d [Max]; int adj [Max]; bool in_q [Max]; int CNT [Max]; int size; int n, m; void add_edge (int u, int V, int cost) {node [size]. V = V; node [size]. cost = cost; node [size]. next = adj [u]; adj [u] = size ++;} struct CMP {bool ope Rator () (const Int & A, const Int & B) {return d [a]> d [B] ;}}; priority_queue <int, vector <int>, CMP> q; void Dijkstra () {for (INT I = 0; I <= N; I ++) d [I] = inf; q. push (1); D [1] = 0; int U, V, W; while (! Q. Empty () {u = Q. Top (); q. Pop (); For (INT I = adj [u]; I! =-1; I = node [I]. next) {v = node [I]. v; W = node [I]. cost; If (d [v]> d [u] + W) {d [v] = d [u] + W; q. push (v) ;}}} void spfa () {memset (in_q, false, sizeof (in_q); For (INT I = 1; I <= N; I ++) d [I] = inf; stack <int> S; d [1] = 0; S. push (1); in_q [1] = true; int U, V, W; while (! S. Empty () {u = S. Top (); S. Pop (); in_q [u] = false; For (INT I = adj [u]; I! =-1; I = node [I]. next) {v = node [I]. v; W = node [I]. cost; If (d [v]> d [u] + W) {d [v] = d [u] + W; If (! In_q [v]) {in_q [v] = true; S. push (v) ;}}}} int main () {int A, B, W; scanf ("% d", & N, & M ); for (INT I = 0; I <= N; I ++) adj [I] =-1; for (INT I = 0; I <m; I ++) {scanf ("% d", & A, & B, & W); add_edge (a, B, W);} Dijkstra (); printf ("% d \ n", d [N]); 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.