POJ-1364 King

来源:互联网 发布:笔记本电脑测评软件 编辑:程序博客网 时间:2024/04/30 07:43
King
Time Limit: 1000MS Memory Limit: 10000K   

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 many 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 decision about it would 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 would 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 + aSi+1 + ... + aSi+ni of each subsequence Si an integer constraint ki (i.e. aSi + aSi+1 + ... + aSi+ni < ki or aSi + 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 could 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 would 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 except 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 particular 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
————————————————————紧张的分割线————————————————————
前言:换了机械键盘好开心~~该题就难在题目难读,而且题目的格式还有问题。不是Si+1而是S[i+1]。
题意:不用管神马Si不Si的。其实就是对于一个集合:
a[1],a[2],a[3],a[4],...,a[n]
输入:s, n, o, k
a[s]+a[s+1]+...+a[s+n] o k
思路:差分约束模板题,
把a[i]当做一个区间,再把区间转化成边权。即:
S[0], S[1], S[2], ... , S[n]
S[i] - S[i-1]就代表a[i]了。因此对于a[s]~a[s+n]的和,就是S[s+n] - S[s-1]了。
S[s+n] - S[s-1] > k 或 S[s+n] - S[s-1] < k
因为差分约束只能用 >= 或者 <= ,因此处理一下k。
本题无所谓于跑最短路还是跑最长路。要求的是若干连通分量中是否存在负环。(本题中给出的约束关系可能构成不只一张连通图),因此所有的连通分量都要跑。
新增加一个超级源点S[n+1],添加它向其它所有顶点的有向边,权值为0。如此即可跑完所有连通分量。
代码如下:
/*ID: j.sure.1PROG:LANG: C++*//****************************************/#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>#include <stack>#include <queue>#include <vector>#include <map>#include <string>#include <climits>#include <iostream>#define INF 0x3f3f3f3fusing namespace std;/****************************************/const int N = 110;struct Node {int v, w, next;}edge[N*N];int n, m;int q[N], head[N], dis[N], enq[N], tot;bool inq[N];void add(int u, int v, int w) {edge[tot].v = v;edge[tot].w = w;edge[tot].next = head[u];head[u] = tot++;}bool spfa(int st) {for(int i = 0; i <= n; i++) {dis[i] = INF;inq[i] = enq[i] = 0;}dis[st] = 0;int fron = 0, rear = 0;q[rear++] = st;inq[st] = 1;enq[st]++;while(fron < rear) {int u = q[fron%N]; fron++;inq[u] = false;for(int 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(!inq[v]) {q[rear%N] = v; rear++;inq[v] = true;enq[v]++;if(enq[v] > st+1) return false;}}}}return true;}int main(){#ifdef J_Sure//freopen("000.in", "r", stdin);//freopen(".out", "w", stdout);#endifwhile(~scanf("%d", &n), n) {scanf("%d", &m);tot = 0;memset(head, -1, sizeof(head));int i, nn, k;char oo[5];while(m--) {scanf("%d%d%s%d", &i, &nn, oo, &k);int j = i + nn; i--;//ai+...+aj >= k+1//s[j]-s[i] >= k+1if(oo[0] == 'g') {add(j, i, -k-1);}else {add(i, j, k-1);}}for(int i = 0 ; i < n+1; i++)add(n+1, i, 0);if(spfa(n+1)) puts("lamentable kingdom");else puts("successful conspiracy");}return 0;}


0 0
原创粉丝点击