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 2
1 2 gt 0
2 2 lt 2
1 2
1 0 gt 0
1 0 lt 0
0

Sample Output

lamentable kingdom
successful conspiracy

我们令S[i]= a1+a2+..+ai. 所以对于每一条约束条件比如:

a[si]+a[si+1]+…+a[si+ni]< ki . 我们可以转化为 S[si+ni] – S[si-1] <= ki-1.

这样就可以转化为了差分约束系统了.

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define INF 1e9
using namespace std;
const int maxn=100+10;
const int maxm=10000+10; struct Edge
{
int from,to,dist;
Edge(){}
Edge(int f,int t,int d):from(f),to(t),dist(d){}
}; struct BellmanFord
{
int n,m;
int head[maxn],next[maxm];
Edge edges[maxm];
int d[maxn];
bool inq[maxn];
int cnt[maxn]; void init(int n)
{
this->n=n;
m=0;
memset(head,-1,sizeof(head));
} void AddEdge(int from,int to,int dist)
{
edges[m]=Edge(from,to,dist);
next[m]=head[from];
head[from]=m++;
} bool bellmanford(int f)
{
memset(inq,0,sizeof(inq));
memset(cnt,0,sizeof(inq));
queue<int> Q;
for(int i=0;i<n;i++) d[i]= i==f?0:INF;
Q.push(f); while(!Q.empty())
{
int u=Q.front(); Q.pop();
inq[u]=false;
for(int i=head[u];i!=-1;i=next[i])
{
Edge &e=edges[i];
if(d[e.to] > d[u]+e.dist)
{
d[e.to] = d[u]+e.dist;
if(!inq[e.to])
{
inq[e.to]=true;
Q.push(e.to);
if(++cnt[e.to]>n) return true;
}
}
}
}
return false;
}
}BF; int main()
{
int n,m;
while(scanf("%d",&n)==1&&n)
{
scanf("%d",&m);
BF.init(n+2);
while(m--)
{
int si,ni,ki;
char str[10];
scanf("%d%d%s%d",&si,&ni,str,&ki);
if(str[0]=='l') BF.AddEdge(si-1,si+ni,ki-1);
else if(str[0]=='g') BF.AddEdge(si+ni,si-1,-ki-1);
}
for(int i=0;i<=n;i++) //超级源到其他所有点的边
BF.AddEdge(n+1,i,0);
printf("%s\n",BF.bellmanford(n+1)?"successful conspiracy":"lamentable kingdom");
}
return 0;
}

图论--差分约束--POJ 1364 King的更多相关文章

  1. 图论--差分约束--POJ 3159 Candies

    Language:Default Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 43021   Accep ...

  2. 图论--差分约束--POJ 3169 Layout(超级源汇建图)

    Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 < ...

  3. 图论--差分约束--POJ 2983--Is the Information Reliable?

    Description The galaxy war between the Empire Draco and the Commonwealth of Zibu broke out 3 years a ...

  4. 图论--差分约束--POJ 1201 Intervals

    Intervals Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 30971 Accepted: 11990 Descripti ...

  5. POJ 1364 King (UVA 515) 差分约束

    http://poj.org/problem?id=1364 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemi ...

  6. poj 1364 King(差分约束)

    题意(真坑):傻国王只会求和,以及比较大小.阴谋家们想推翻他,于是想坑他,上交了一串长度为n的序列a[1],a[2]...a[n],国王作出m条形如(a[si]+a[si+1]+...+a[si+ni ...

  7. POJ 1364 King (差分约束)

    King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8660   Accepted: 3263 Description ...

  8. POJ 1364 King --差分约束第一题

    题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析 ...

  9. [poj 1364]King[差分约束详解(续篇)][超级源点][SPFA][Bellman-Ford]

    题意 有n个数的序列, 下标为[1.. N ], 限制条件为: 下标从 si 到 si+ni 的项求和 < 或 > ki. 一共有m个限制条件. 问是否存在满足条件的序列. 思路 转化为差 ...

随机推荐

  1. go 内置函数

    一.什么是内置函数? 二.内置函数有哪些? 名称 说明 close 用于管道通信 len.cap len 用于返回某个类型的长度或数量(字符串.数组.切片.map 和管道):cap 是容量的意思,用于 ...

  2. 跨域cookies 共享

    这是由于,本地调试.涉及到cookies的问题 想要跨域使用的问题 vue 中的mian.js中放入下面代码 import axios from 'axios' axios.defaults.with ...

  3. 【Server】Windows系统安装Tomcat服务器

    安装Tomcat服务器 Tomcat服务器地址:https://tomcat.apache.org/download-80.cgi 当前版本点选8以上版本,最新的可能不稳定,所以选8或者9版本 直接解 ...

  4. 基于 HTML5 WebGL 的 CPU 监控系统

    前言 科技改变生活,科技的发展带来了生活方式的巨大改变.随着通信技术的不断演进,5G 技术应运而生,随时随地万物互联的时代已经来临.5G 技术不仅带来了更快的连接速度和前所未有的用户体验,也为制造业, ...

  5. 干货:python面对对象类继承简介

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:python视觉算法 PS:如有需要Python学习资料的小伙伴可以加 ...

  6. 遇到自己喜欢的视频无法下载,python帮你解决

    问题描述 python是一种非常好用的爬虫工具.对于大多数的爬虫小白来说,python是更加简洁,高效的代码.今天就用实际案例讲解如何爬取动态的网站视频. 环境配置:python3:爬虫库reques ...

  7. spring源码阅读笔记08:bean加载之创建bean

    上文从整体视角分析了bean创建的流程,分析了Spring在bean创建之前所做的一些准备工作,并且简单分析了一下bean创建的过程,接下来就要详细分析bean创建的各个流程了,这是一个比较复杂的过程 ...

  8. L21 Momentum RMSProp等优化方法

    airfoil4755 下载 链接:https://pan.baidu.com/s/1YEtNjJ0_G9eeH6A6vHXhnA 提取码:dwjq 11.6 Momentum 在 Section 1 ...

  9. JavaScript的运行机制!!!很重要很重要!!!!!!请看大神操作!

    https://juejin.im/post/59e85eebf265da430d571f89

  10. SpringBoot word 转换为 pdf

    转换文件 swagger 地址, 基于 SpringBoot 开发 http://119.27.167.41:8888/convertor/swagger-ui.html 带有图片的word 转换体验 ...