POJ-1364 King 【差分约束】

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



题解:

差分约束系统,要注意的是这题要把小于号转化为小于等于(可以利用整数相减的性质改变k),另外就是图中的点包括0因此要把每个点的下标都+1让0变成1,才能让0当做源点。



代码:
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<queue>
 6 #include<string>
 7 using namespace std;
 8 #define M(a, b) memset(a, b, sizeof(a))
 9 #define INF 0x3f3f3f3f
10 const int N = 100 + 5;
11 int d[N], cnt[N], n, m;
12 bool inq[N];
13 struct Node {
14     int to, w;
15 };
16 vector<Node> G[N];
17 
18 void Init() {
19     for (int i = 0; i <= n+2; ++i) G[i].clear();
20 }
21 
22 void AddEdge(int from, int to, int w) {
23     G[from].push_back(Node{to, w});
24 }
25 
26 bool spfa(int s, int n) {
27     M(d, INF); M(inq, 0); M(cnt, 0);
28     d[s] = 0; cnt[s] = 1;
29     queue<int> q;
30     q.push(s); inq[s] = 1;
31     while (!q.empty()) {
32         int u = q.front(); q.pop();
33         inq[u] = 0;
34         for (int i = 0; i < G[u].size(); ++i) {
35             Node &e = G[u][i];
36             if (d[e.to] > d[u] + e.w) {
37                 d[e.to] = d[u] + e.w;
38                 if (inq[e.to]) continue;
39                 q.push(e.to); inq[e.to] = 1;
40                 if (++cnt[e.to] > n) return false;
41             }
42         }
43     }
44     return true;
45 }
46 
47 int main() {
48     while (~scanf("%d", &n), n) {
49         Init();
50         scanf("%d", &m);
51         int u, v, k;
52         char o[3];
53         for (int i = 1; i <= m; ++i) {
54             scanf("%d%d%s%d", &u, &v, o, &k);
55             if (o[0] == 'g') AddEdge(u+v+1, u, -k-1);
56             else AddEdge(u, u+v+1, k-1);
57         }
58         for (int i = 1; i <= n+1; ++i) AddEdge(0, i, 0);
59         if (spfa(0, n+2)) printf("lamentable kingdom\n");
60         else printf("successful conspiracy\n");
61     }
62 
63     return 0;
64 }

 


posted @ 2017-04-06 22:19  Robin!  阅读(187)  评论(0编辑  收藏  举报