POJ1364 King(差分约束+Spfa)

来源:互联网 发布:傲剑十八铜仁数据 编辑:程序博客网 时间:2024/04/29 14:19

题目链接:http://poj.org/problem?id=1364


King
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 9573 Accepted: 3575

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起ni项之和大于(gt)或小于(lt)一个数ki,问是否存在这个数列。


差分约束,根据题意列出差分约束方程:

1. si起ni项之和大于ki:sum[si+ni]-sum[si-1]>ki。转化为小于等于:sum[si-1]-sum[si+ni]<=-(ki+1)。

2.si起ni项之和小于ki:sum[si+ni]-sum[si-1]<ki。转化为小于等于:sum[si+ni]-sum[si-1]<=ki-1。

3.设置起点为n+1:sum[i]<=sum[n+1]。转化为小于等于:sum[i]-sum[n+1]<=0。


注意上述式子中,前一项为路径终点,后一项为起点,不等号右边为权值,满足关系为最短路(dis[v]>dis[u]+cost[i])


建立差分约束系统后,利用Spfa最短路,判断有无负环,有负环则输出successful conspiracy。


#include <iostream>#include <cstdio>#include <cstring>#include <string>using namespace std;const int mxn=105;const int mxq=10005;int first[mxn],nxt[mxn<<1],vv[mxn<<1],cost[mxn<<1],e;//数组模拟邻接表int n,m,si,ni,ki;char oi[5];int q[mxq],dis[mxn],vis[mxn],cs[mxn],head,tail,cnt;bool flag;void add(int u,int v ,int c){vv[e]=v;cost[e]=c;nxt[e]=first[u];first[u]=e++;}void spfa(int s){memset(dis,0x3f,sizeof(dis));memset(vis,0,sizeof(vis));memset(cs,0,sizeof(cs));dis[s]=0;vis[s]=1;cs[s]=1;q[tail]=s;tail=(tail+1)%mxq;++cnt;while (cnt>0){int u=q[head];head=(head+1)%mxq;--cnt;vis[u]=0;for (int i=first[u];i!=-1;i=nxt[i]){int v=vv[i];if (dis[v]>dis[u]+cost[i]){dis[v]=dis[u]+cost[i];if (vis[v]==0){vis[v]=1;q[tail]=v;tail=(tail+1)%mxq;++cnt;cs[v]++;if (cs[v]>n+2) {flag=0;return;}//入队次数大于总结点个数,说明存在负环。}}}}}int main(){while (scanf("%d",&n)!=EOF && n!=0){scanf("%d",&m);memset(first,-1,sizeof(first));e=0;for (int i=0;i<=n;i++) add(n+1,i,0);//差分约束关系3for (int i=0;i<m;i++){scanf("%d%d",&si,&ni);cin>>oi>>ki;if (oi[0]=='l') add(si-1,si+ni,ki-1);else add(si+ni,si-1,-ki-1);//差分约束关系1和2}flag=1;spfa(n+1);if (flag==1) printf("lamentable kingdom\n");else printf("successful conspiracy\n");}return 0;}


0 0
原创粉丝点击