Poj 1364 King (difference constraint)

Source: Internet
Author: User
Tags integer numbers
King
Time limit:1000 ms   Memory limit:10000 K
Total submissions:8919   Accepted:3353

Description

Once, in one kingdom, there was a queen and that Queen was expecting a baby. the Queen prayed: ''if my child was a son and if only he was a sound king. ''after nine months her child was born, and indeed, she gave birth to a nice son.
Unfortunately, as it used to happen in royal families, the son was a little retarded. after creating years of study he was able just to add integer numbers and to compare whether the result is greater or less than a given integer number. in addition, the numbers
Had to be written in a sequence and he was able to sum just continuous subsequences of the sequence.

The old king was very unhappy of his son. but he was ready to make everything to enable his son to govern the kingdom after his death. with regards to his son's skills he decided that every problem the king had to decide about had to be presented in a form
Of a finite sequence of integer numbers and the demo-about it wocould be done by stating an integer constraint (I. e. an upper or lower limit) for the sum of that sequence. in this way there was at least some hope that his son wocould be able to make some decisions.

After the old king died, the young king began to reign. but very soon, a lot of people became very unsatisfied with his decisions and decided to dethrone him. they tried to do it by proving that his decisions were wrong.

Therefore some conspirators presented to the young king a set of problems that he had to decide about. the set of problems was in the form of subsequences Si = {ASI, ASI + 1 ,..., ASI + Ni} of a sequence S = {A1, A2 ,..., an }. the king thought a minute and then
Decided, I. e. he set for the sum ASI + 1 +... + ASI + Ni of each subsequence Si an integer constraint ki (I. e. ASI + 1 +... + ASI + Ni <Ki Or ASI + 1 +... + ASI + Ni> Ki resp .) and declared these constraints as his decisions.

After a while he realized that some of his decisions were wrong. he cocould not revoke the declared constraints but trying to save himself he decided to fake the sequence that he was given. he ordered to his advisors to find such a sequence s that wowould satisfy
The constraints he set. Help the advisors of the King and write a program that decides whether such a sequence exists or not.

Input

The input consists of blocks of lines. each block should t the last corresponds to one set of problems and King's decisions about them. in the first line of the block there are integers n, and m where 0 <n <= 100 is length of the sequence s and 0 <m <= 100
Is the number of subsequences Si. next m lines contain particle decisions coded in the form of quadruples Si, Ni, oi, KI, where oi represents operator> (coded as GT) or operator <(coded as Lt) respectively. the symbols Si, Ni and Ki have the meaning described
Above. The last block consists of just one line containing 0.

Output

The output contains the lines corresponding to the blocks in the input. A line contains text successful conspiracy when such a sequence does not exist. otherwise it contains text lamentable kingdom. there is no line in the output corresponding to the last ''null''
Block of the input.

Sample Input

4 21 2 GT 02 2 lt 21 21 0 GT 01 0 lt 00

Sample output

 
Lamentable kingdomsuccessful conspiracy

Source

Central Europe 1997

Ideas:

Use the relationship between the first n items and the Sn to establish a constraint, and use spfa () to determine whether a negative ring exists.


Note:

1. the relationship between S (U-1) and S (U + V) is '<' or '>, the relationship between "> =" and "<=" must be changed from "1" to "> =" or "<=.

2. The graph can be unconnected, so you need to enumerate the start point.


Code:

# Include <iostream> # include <cstdio> # include <cstring> # include <queue> # define maxn 2005 using namespace STD; int n, m, ANS, CNT, Sx, ex; bool vis [maxn]; char s [maxn]; int Dist [maxn], p [maxn], num [maxn]; struct node {int R, cost; int next;} edge [maxn]; queue <int> q; const int INF = 0x3f3f3f; void Init () {memset (p,-1, sizeof (p ));} void addedge (int u, int V, int W) {CNT ++; edge [CNT]. R = V; edge [CNT]. cost = W; edge [CNT]. next = P [u ]; P [u] = CNT;} bool spfa () {int I, j, nx; memset (VIS, 0, sizeof (VIS); memset (Dist, 0x3f, sizeof (DIST); memset (Num, 0, sizeof (Num); While (! Q. empty () Q. pop (); Dist [SX] = 0; vis [SX] = 1; num [SX] ++; q. push (SX); While (! Q. Empty () {Nx = Q. Front (); vis [NX] = 0; q. Pop (); for (I = P [NX]; I! =-1; I = edge [I]. next) {If (Dist [edge [I]. r]> Dist [NX] + edge [I]. cost) {Dist [edge [I]. r] = DIST [NX] + edge [I]. cost; If (! Vis [edge [I]. r]) {vis [edge [I]. r] = 1; num [edge [I]. r] ++; If (Num [edge [I]. r]> N) return true; q. push (edge [I]. r) ;}}}return false;} int main () {int I, j, U, V, W, flag; while (scanf ("% d ", & N), n) {scanf ("% d", & M); Init (); for (I = 1; I <= m; I ++) {scanf ("% d % S % d", & U, & V, S, & W); If (strcmp (S, "GT") = 0) addedge (U + V, U-1,-W-1); else addedge (U-1, U + V, W-1);} flag = 0; for (I = 0; I <= N; I ++) {SX = I; If (spfa () {flag = 1; break ;}} if (FLAG) printf ("successful conspiracy \ n "); else printf ("lamentable Kingdom \ 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.