首页 > 代码库 > poj1364 King

poj1364 King

地址:http://poj.org/problem?id=1364

题目:

King
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 13466   Accepted: 4793

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

Central Europe 1997
 
思路:
  从前缀和的角度考虑,每个给出的条件都是一个不等式。
  如: 1 2 gt 0 => s3-s1>0 => s3-s1>=1
    2 2 lt 2 => s4-s2<2 => s4-s2<=1
  这样问题变成了判断这个不等式组是否成立的问题了,也是差分约束问题。
  跑个 最长路,看有没有环就好了。为了防止图不连通,增加一个超级原点,从原点到其他点建一条权值为0的边。
  对了,别用vector存边,会T。
 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cmath>
 4 #include <queue>
 5 using namespace std;
 6 
 7 #define MP make_pair
 8 #define PB push_back
 9 typedef long long LL;
10 typedef pair<int,int> PII;
11 const double eps=1e-8;
12 const double pi=acos(-1.0);
13 const int K=1e6+7;
14 const int mod=1e9+7;
15 
16 struct node
17 {
18     int to,v,next;
19 }edge[K];
20 int tot,head[K];
21 void add(int x,int y,int z)
22 {
23     edge[tot].to=y,edge[tot].v=z,edge[tot].next=head[x];
24     head[x]=tot++;
25 }
26 int n,m,vis[K],dis[K],cnt[K];
27 int spfa(int st)
28 {
29     queue<int>q;
30     q.push(st),vis[st]=1,dis[st]=1,cnt[st]=1;
31     while(q.size())
32     {
33         int u=q.front();
34         vis[u]=0;q.pop();
35         for(int i=head[u];~i;i=edge[i].next)
36         {
37             int v=edge[i].to,w=edge[i].v;
38             if(dis[v]<dis[u]+w)
39             {
40                 dis[v]=dis[u]+w;
41                 if(vis[v])  continue;
42                 if(cnt[v]>n) return 1;
43                 q.push(v),vis[v]=1,cnt[v]++;
44             }
45         }
46     }
47     return 0;
48 }
49 int main(void)
50 {
51     char ss[20];
52     while(~scanf("%d%d",&n,&m)&&n)
53     {
54         tot=0;
55         for(int i=0;i<=n+1;i++)
56             head[i]=-1,vis[i]=0,dis[i]=0,cnt[i]=0;
57         for(int i=1,u,v,w;i<=m;i++)
58         {
59             scanf("%d%d%s%d",&u,&v,ss,&w);
60             if(ss[0]==g)  add(u-1,u+v,w+1);
61             else add(u+v,u-1,1-w);
62         }
63         for(int i=0;i<=n;i++)
64             add(n+1,i,0);
65         if(spfa(n+1))
66             printf("successful conspiracy\n");
67         else
68             printf("lamentable kingdom\n");
69     }
70     return 0;
71 }

 

poj1364 King