Tag: Stack poj
CandiesCrawling in process...Crawling failedTime limit:1500 MsMemory limit:131072kb64bit Io format:% I64d & % i64u
Submitstatus
Description
During the Kindergarten days, flymouse was the monitor of his class. occasionally the head-teacher brought the kids of flymouse's class a large bag of candies and had flymouse distribute them. all the kids loved candies very much and often compared the numbers of candies they got with others. A kid a coshould had the idea that though it might be the case that another kid B was better than him in some aspect and therefore had a reason for deserving more candies than he did, he shoshould never get a certain number of candies fewer than B did no matter how many candies he actually got, otherwise he wowould feel dissatisfied and go to the head-teacher to complain about flymouse's biased distribution.
Snoopy shared class with flymouse at that time. flymouse always compared the number of his candies with that of Snoopy's. he wanted to make the difference between the numbers as large as possible while keeping every kid satisfied. now he had just got another bag of candies from the head-Teacher, what was the largest difference he cocould make out of it?
Input
The input contains a single test cases. The test cases starts with a line with two integersNAndMNot exceeding 30 000 and 150 000 respectively.NIs the number of kids in the class and the kids were numbered 1 throughN. Snoopy and flymouse were always numbered 1 andN. Then followMLines each holding three IntegersA,BAndCIn order, meaning that kidABelieved that kidBShocould never get overCCandies more than he did.
Output
Output one line with only the largest difference desired. The difference is guaranteed to be finite.
Sample Input
2 21 2 52 1 4
Sample output
5
Hint
32-bit signed integer type is capable of doing all arithmetic.
Question. Spfa + forward star learned in the last few days. This question was used in a decisive manner. The data was correct, and the data was handed in happily. TLE then cried. Later I used SLF to optimize it, or TLE. It was really crying. Then I looked at the poj discussion board, and the big guys said they would use the stack, and sad looked at the stack again. Fortunately, it was similar to the queue usage.
Q: Send candy to N people and give M group data. Each group contains three numbers: A, B, and C. A has no more candy than B, that is, the number of candy in B-the number of candy in a <= C. Finally, calculate the maximum number of sweets for N to 1.
Idea: this is a typical difference constraint. Consider the number of sweets as the distance, and regard the maximum number of different sweets as the weight of directed edge AB. We get dis [B]-Dis [a] <= W (, B ). Here we think of the relaxation technique for finding the shortest path, that is, if (DIS [B]> dis [a] + W (a, B ), dis [B] = dis [a] + W (A, B ). That is, to satisfy the condition dis [B]-Dis [a] <= W (A, B) in the question, because we want to maximize dis [B, therefore, this question can be converted into the shortest path. (Language organization is a little poor, so I had to use another idea) PS: I have a reposted topic in my blog, so I can learn what I don't understand.
# Include <stdio. h> # include <string. h> # include <stdlib. h> # include <iostream> # include <queue> # include <stack> # include <algorithm> # define INF 9999999 using namespace STD; int head [30010]; int dis [30010]; int vis [30010]; int n, m, I; int CNT; struct node {int V, W; int next;} edge [150000]; void add (int u, int V, int W) // Add edge {edge [CNT]. V = V; edge [CNT]. W = W; edge [CNT]. next = head [u]; head [u] = CNT ++;} void spfa () {memset (DIS, INF, sizeof (DIS); memset (VIS, 0, sizeof (VIS); stack <int> G; // queue dis [1] = 0; vis [1] = 1; G. push (1); While (! G. Empty () {int u = G. Top (); G. Pop (); vis [u] = 0; for (I = head [u]; I! =-1; I = edge [I]. next) {int v = edge [I]. v; If (DIS [v]> dis [u] + edge [I]. w) {dis [v] = dis [u] + edge [I]. w; If (! Vis [v]) {vis [v] = 1; G. push (v) ;}}} printf ("% d \ n", DIS [N]);} int main () {int U, V, W; while (~ Scanf ("% d", & N, & M) {CNT = 0; memset (Head,-1, sizeof (head); for (I = 1; I <= m; I ++) {scanf ("% d", & U, & V, & W); add (u, v, w) ;}spfa () ;}return 0 ;}
Zookeeper
Candies (differential constraint _ stack + spfa)