Poj 1364 King (differential constraint) (medium)

Source: Internet
Author: User
Tags integer numbers

Poj 1364 King (differential constraint) (medium)
King

Time Limit:1000 MS   Memory Limit:10000 K
Total Submissions:11028   Accepted:4040

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 wocould 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

Idea: The only difficulty of this question is probably understanding the question .. First I. e. aSi + 1 +... + aSi + ni <ki or aSi + 1 +... + aSi + ni> ki resp. wrong. No formula can be pushed. After reading other people's correct questions, I knew it was very simple. Here we will copy the wangjian8006 question below:

Suppose there is a sequence like this: S = {a1, a2, a3, a4... ai...}
Here, ai = a * si, which can be ignored.
Now, an inequality is given so that ai + a (I + 1) + a (I + 2) +... + a (I + n) Ki
First, two numbers represent the number of S sequences and the number of inequalities.
The inequality can be described in this way.
Given four parameters, the first number I can represent the number of the sequence, and then gives n, so that the first two numbers can be described as ai + a (I + 1) +... a (I + n), that is, the continuous sum from I to n, then a symbol and a ki
If the symbol is gt, it indicates '>', and if the symbol is lt, it indicates '<'
The sample can represent
1 2 gt 0
A1 + a2 + a3> 0
2 2 lt 2
A2 + a3 + a4 <2
Finally, I would like to ask if all your inequalities meet the conditions. If the output lamentable kingdom does not meet the output successful conspiracy, I should pay attention to it. Do not reverse it.

 

Solution: a typical difference constraint makes it easy to introduce a constraint inequality.

First set Si = a1 + a2 + a3 +... + ai

According to the example
S3-S0> 0 ----> S0-S3 <=-1
S4-S1 <2 ----> S4-S1 <= 1
Because the condition of the difference constraint is less than or equal to, we can get an equal sign for the ki-1.
The general expression can be expressed
A B gt c
S [A-1]-s [a + B] <=-ki-1
A B lt c
S [a + B]-S [A-1] <= ki-1

Create a Graph Based on the difference constraint and add these directed edges.
Gt: =-ki-1
Lt: = ki-1
Then, judge whether there is a negative ring based on bellman_ford.
If a negative ring exists, this sequence does not satisfy all inequalities.


Code:
//212K0MS#include
 
  #include
  
   #include
   
    using namespace std;#define maxx 105struct edge{    int u,v,w;}edge[maxx];int n,m;int dist[maxx];bool bellman_ford(){    memset(dist,0,sizeof(dist));    for(int i=0;i
    
     >n,n)    {        cin>>m;        for(int i=0;i
     
      

 

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.