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

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

Source

差分约束判是否有负环

设S[j]=a0+a1+a2+...+aj

由题意:S[si+ni]-S[si-1]>k或S[si+ni]-S[si-1]<k,由于差分约束系统只能解决>=和<=的情况,要把>k转换成>=k+1,<k转换成<=k-1

差分约束加入附加源点是为了保证图的连通性,但也可以不加附加源点,而是一开始把所有顶点都入队,并设dis为0(这个时候不算入队次数),相当于超级源点的权值

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath> using namespace std; const int N=;
const int INF=0x3f3f3f3f; struct Edge{
int to,nxt;
int cap;
}edge[N*N]; int n,m,cnt,head[N];
int dis[N],vis[N],count[N]; void addedge(int cu,int cv,int cw){
edge[cnt].to=cv; edge[cnt].cap=cw; edge[cnt].nxt=head[cu];
head[cu]=cnt++;
} int SPFA(){
queue<int> q;
while(!q.empty())
q.pop();
int limit=(int)sqrt(1.0*n);
memset(vis,,sizeof(vis));
memset(count,,sizeof(count));
for(int i=;i<=n;i++){
dis[i]=;
q.push(i);
//vis[i]=1;
}
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].nxt){
int v=edge[i].to;
if(dis[v]>dis[u]+edge[i].cap){
dis[v]=dis[u]+edge[i].cap;
if(!vis[v]){
vis[v]=;
if(++count[v]>limit)
return ;
q.push(v);
}
}
}
}
return ;
} int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d",&n) && n){
cnt=;
memset(head,-,sizeof(head));
scanf("%d",&m);
int u,v,w;
char op[];
while(m--){
scanf("%d%d%s%d",&u,&v,op,&w);
if(op[]=='g')
addedge(v+u,u-,-w-);
else
addedge(u-,u+v,w-);
}
if(SPFA())
puts("lamentable kingdom");
else
puts("successful conspiracy");
}
return ;
}

POJ 1364 King (差分约束)的更多相关文章

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

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

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

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

  3. poj 1364 King(差分约束)

    题目:http://poj.org/problem?id=1364 #include <iostream> #include <cstdio> #include <cst ...

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

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

  5. poj 1364 King(差分约束)

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

  6. POJ 1364 King

    http://poj.org/problem?id=1364 题意 :给出一个序列a1,a2,a3,a4.....ai,......at ;然后给你一个不等式使得ai+a(i+1)+a(i+2)+.. ...

  7. poj 1201 Intervals(差分约束)

    题目:http://poj.org/problem?id=1201 题意:给定n组数据,每组有ai,bi,ci,要求在区间[ai,bi]内至少找ci个数, 并使得找的数字组成的数组Z的长度最小. #i ...

  8. poj 1201 Intervals——差分约束裸题

    题目:http://poj.org/problem?id=1201 差分约束裸套路:前缀和 本题可以不把源点向每个点连一条0的边,可以直接把0点作为源点.这样会快许多! 可能是因为 i-1 向 i 都 ...

  9. POJ——1364King(差分约束SPFA判负环+前向星)

    King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11946   Accepted: 4365 Description ...

随机推荐

  1. Insertion Sort List Leetcode java

    题目: Sort a linked list using insertion sort. 题解: Insertion Sort就是把一个一个元素往已排好序的list中插入的过程. 初始时,sorted ...

  2. 新鲜出炉!9个超高分辨率的iPhone 6原型素材打包下载

    iPhone 6 出场,设计师又有得忙活了,但是新鲜的资源你们在哪里?!今天我们收集了一组精致的iPhone 6 模型素材,超高分辨率,多种视图,全都打包完毕,点一下就可以拿回家!赶紧来取吧!——   ...

  3. 【Quick-COCOS2D-X 3.3 怎样绑定自己定义类至Lua之中的一个】环境搭建

    * 确定你安装了Android NDK R9B 版本号 ,假设没有前往下面地址下载. ( https://dl.google.com/android/ndk/android-ndk-r9b-linux ...

  4. Back Track 5 之 网络踩点

    DNS记录探测 dnsenum 针对NDS信息收集的工具 格式: ./dnsenum.pl dbsserver (域名) 请原谅我拿freestu.net这个学校团委的域名做的测试,求不黑!! dns ...

  5. Mac下进行基于java服务端语言的微信公众号本地js-sdk调试的大致方法

    开发微信公众号应用调用js-sdk,需要先在微信公众号后台配置可信域名,之后从微信的入口地址重定向到改域名下的路径后便会返回code,之后可以拿到一系列需要的参数等等.那么本地开发,如果使用的是PHP ...

  6. [Node.js] Level 3 new. Steam

    File Read Stream Lets use the fs module to read a file and log its contents to the console. Use the  ...

  7. [Backbone]5. Model & View, toggle between Models and Views -- 2

    Dr. Goodparts is pretty flaky and has been cancelling a lot of appointments lately. He's asked for a ...

  8. 【statistics】理想论坛2018-4-25日统计

    说明:利用理想论坛爬虫1.07版(http://www.cnblogs.com/xiandedanteng/p/8954115.html) 下载了前十页主贴及子贴,共得到359619条数据,以此数据为 ...

  9. ZH奶酪:Ubuntu启动/重启/停止apache服务

    Start Apache 2 Server /启动apache服务 # /etc/init.d/apache2 start or $ sudo /etc/init.d/apache2 start Re ...

  10. MongodbUtil

    import com.mongodb.MongoClient; import com.mongodb.MongoClientOptions; import com.mongodb.MongoClien ...