【POJ 1364】King(差分约束系统)

来源:互联网 发布:淘宝卖衣服店铺名 编辑:程序博客网 时间:2024/06/01 17:32

【POJ 1364】King(差分约束系统)

King
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 12033 Accepted: 4394

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

Source

Central Europe 1997

题目大意:

给出一个序列S的长度n

还有m个关系 si ni oi ki

oi为gt时表示 S[si]+S[si+1]+S[si+2]+....+S[si+ni] > ki

oi为lt时表示 S[si]+S[si+1]+S[si+2]+....+S[si+ni] < ki

如果存在满足这m个关系的序列S 输出“lamentable kingdom”

否则 输出“successful conspiracy”


主要是建图。直接根据公式不好建图,可以把点的含义转换以下

变成sum[i],表示S[1]到S[i]的和。sum[0]即为空(0)。

这样 对于si ni gt ki就转换为 sum[si+ni]-sum[si-1] >= ki-1(边权是要包含的

si ni lt ki 转换为 sum[si+ni+]-sum[si-1] < ki 保持统一 变为 sum[si-1]-sum[si+ni] >= -ki-1


这样按条件建图就可。

差分约束中,看关系是否互斥,即判断是否有负权环。

跑一边Bellman即可


代码如下:

#include <iostream>#include <cmath>#include <vector>#include <cstdlib>#include <cstdio>#include <cstring>#include <queue>#include <stack>#include <list>#include <algorithm>#include <map>#include <set>#define LL long long#define Pr pair<int,int>#define fread(ch) freopen(ch,"r",stdin)#define fwrite(ch) freopen(ch,"w",stdout)using namespace std;const int INF = 0x3f3f3f3f;const int msz = 10000;const int mod = 1e9+7;const double eps = 1e-8;int dis[233];int mp[233][233];int n;bool Bellman_Ford(){memset(dis,0,sizeof(dis));for(int k = 1; k < n; ++k)for(int i = 0; i < n; ++i)for(int j = 0; j < n; ++j)if(dis[j] > dis[i]+mp[i][j]) dis[j] = dis[i]+mp[i][j];for(int i = 0; i < n; ++i)for(int j = 0; j < n; ++j)if(dis[j] > dis[i]+mp[i][j]) return false;return true;}int main(){//fread("");//fwrite("");int m;int u,v,w;char opt[5];while(~scanf("%d",&n) && n){scanf("%d",&m);memset(mp,INF,sizeof(mp));while(m--){scanf("%d%d%s%d",&u,&v,opt,&w);v = u+v;if(opt[0] == 'l') mp[u-1][v] = min(mp[u-1][v],w-1);else mp[v][u-1] = min(mp[v][u-1],-w-1);}n += 1;puts(Bellman_Ford()? "lamentable kingdom": "successful conspiracy");}return 0;}




0 0
原创粉丝点击