Poj1364 King [difference constraint]

Source: Internet
Author: User

King
Time limit:1000 ms   Memory limit:10000 K
Total submissions:9977   Accepted:3711

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

Source

Central Europe 1997 reference http://blog.csdn.net/wangjian8006/article/details/7956848
Question: Convert the condition given by the question to the form of Xi-XJ <= W;, and then create a virtual node 0. The distance from the node to each vertex is 0, use spfa to determine whether the image has a negative ring.


#include <stdio.h>#include <string.h>#include <queue>#define maxn 106#define inf 0x3f3f3f3fusing std::queue;int head[maxn], out[maxn];struct Node{int to, w, next;} E[maxn << 1];int dist[maxn];bool vis[maxn];void addEdge(int a, int b, int c, int i){E[i].to = b; E[i].w = c;E[i].next = head[a]; head[a] = i;}bool SPFA(int s, int n){int i, u, v, tmp;for(i = 0; i <= n + 1; ++i){dist[i] = inf; out[i] = vis[i] = 0;}queue<int> Q; Q.push(s);vis[s] = 1; dist[s] = 0;while(!Q.empty()){u = Q.front(); Q.pop();if(++out[u] > n) return false;for(i = head[u]; i != -1; i = E[i].next){tmp = dist[u] + E[i].w;v = E[i].to;if(tmp < dist[v]){dist[v] = tmp; if(!vis[v]) Q.push(v);}}}return true;}int main(){int n, m, i, a, b, c;char str[5];while(scanf("%d%d", &n, &m) == 2){memset(head, -1, sizeof(head));for(i = 0; i < m; ++i){scanf("%d%d%s%d", &a, &b, str, &c);if(str[0] == 'g') addEdge(a+b+1, a, -c-1, i);else addEdge(a, b+a+1, c - 1, i);}for(i = 1; i <= n + 1; ++i)addEdge(0, i, 0, m + i - 1);if(SPFA(0, n + 1)) printf("lamentable kingdom\n");else printf("successful conspiracy\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.