POJ 1364 King

King

Time Limit: 1000ms

Memory Limit: 10000KB

This problem will be judged on PKU. Original ID: 1364
64-bit integer IO format: %lld      Java class name: Main

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

解题:差分约束。

设S = a1+a2+a3+...+an

gt:S[a+b] - S[a-1] > C  ------> S[a+b] - S[a-1] >= C+1  -->  S[a+b] - (C+1) >= S[a-1]

lt:S[a+b] - S[a-1] < C  -->  S[a+b] - S[a-1] <= C-1  -->  S[a-1] + C-1 >= S[a+b]

建立超级源点n+1,n+1到其他的点的权值为0.进行spfa。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 struct arc{
18     int to,w;
19     arc(int x = 0,int y = 0){
20         to = x;
21         w = y;
22     }
23 };
24 vector<arc>g[510];
25 int n,m,d[510],cnt[500];
26 bool in[510];
27 queue<int>q;
28 bool spfa(){
29     for(int i = 0; i <= n; i++){
30         d[i] = INF;
31         cnt[i] = 0;
32         in[i] = false;
33         g[n+1].push_back(arc(i,0));
34     }
35     while(!q.empty()) q.pop();
36     d[n+1] = 0;
37     cnt[n+1] = 1;
38     q.push(n+1);
39     in[n+1] = true;
40     while(!q.empty()){
41         int u = q.front();
42         q.pop();
43         in[u] = false;
44         for(int i = 0; i < g[u].size(); i++){
45             if(d[g[u][i].to] > d[u]+g[u][i].w){
46                 d[g[u][i].to] = d[u]+g[u][i].w;
47                 if(!in[g[u][i].to]){
48                     in[g[u][i].to] = true;
49                     if(++cnt[g[u][i].to] > n) return false;
50                     q.push(g[u][i].to);
51                 }
52             }
53         }
54     }
55     return true;
56 }
57 int main() {
58     int i,u,v,w;
59     char str[5];
60     while(scanf("%d",&n),n){
61         scanf("%d",&m);
62         for(i = 0; i < 500; i++) g[i].clear();
63         for(i = 0; i < m; i++){
64             scanf("%d %d %s %d",&u,&v,str,&w);
65             if(str[0] == ‘g‘)
66                 g[u+v].push_back(arc(u-1,-w-1));
67             else g[u-1].push_back(arc(u+v,w-1));
68         }
69         spfa()?puts("lamentable kingdom"):puts("successful conspiracy");
70     }
71     return 0;
72 }

时间: 2024-12-13 21:35:07

POJ 1364 King的相关文章

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

题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析:典型差分约束题,变换,令Ti = SUM(Xj) (0<=j<=i).  则表达式(1)可以看做T(a+b)-T(a-1) > k,也就是T(a-1)-T(a+b) < -k,又因为全是整数,所以T(a-1)-T(a+b) <= -k-1.  同理,(2)看做T(a+b)-T(

POJ 1364 King 差分约束 找负环

嘛,虽然是一道水题+模板题,不过还是学到了很多东西的,记录一下. 首先题目给出的不等式是小于,但是差分约束系统只能处理小于等于的情况,所以要转化成小于等于的进行处理.对于整数处理方法非常简单= = 然后是找负环的情况,其实不需要考虑图连不连通,只要一开始就把所有的点的d置成0,然后都push进队列里面就好了. PS:这种方法同样可以用在处理多源点最短路问题上. #include <cstdio> #include <cstring> #include <cmath> #

poj 1364 King(差分约束)(中等)

King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11028   Accepted: 4040 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 kin

POJ 1364 King(非连通图的差分约束,经典好题)

King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11103   Accepted: 4068 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 kin

poj 1904 King&#39;s Quest

King's Quest 题意:有N个王子和N个妹子;(1 <= N <= 2000)第i个王子喜欢Ki个妹子:(详见sample)题给一个完美匹配,即每一个王子和喜欢的一个妹子结婚:问每一个王子可以有几种选择(在自己喜欢的妹子里面选),并输出可选的妹子的标号(升序): Sample Input 4 (N) 2 1 2 (Ki) 2 1 2 2 2 3 2 3 4 1 2 3 4 (完美匹配) Sample Output 2 1 2 2 1 2 1 3 1 4 分析:图匹配问题,1~N为王子的

Poj 1904 King&#39;s Quest 强连通分量

题目链接: http://poj.org/problem?id=1904 题意: 有n个王子和n个公主,王子只能娶自己心仪的公主(一个王子可能会有多个心仪的公主),现已给出一个完美匹配,问每个王子都可以取哪些公主,并且保证取了一个公主后,全局还是存在完美匹配. 题解: 1.建图: 如果王子u对公主v心仪,则连一条边u->v.在样例给出的那组完美匹配中,如果王子u娶了公主v,连一条边v->u. 2.求强连通分量: 如果王子和自己心仪的公主属于同一个强连通分量,那么王子就可以娶这个公主. 1 #i

POJ - 1904 King&#39;s Quest(强连通分量+二分图匹配)

题目大意:有N个帅哥和N个美女,现在给出每个帅哥所喜欢的美女的编号,和一个帅哥和美女的完美匹配 问每个帅哥可以娶多少个美女,且当他娶完这个美女后,剩下的人还可以完美匹配 解题思路:神题啊,给一个大神的详细解答 具体是这样的,首先先建边,把帅哥和能娶到的美女连边,再把完美匹配的美女和帅哥连边,这样就形成了一张有向图了 接着,找出这张有向图的所有强连通分量,在强连通分量里面的帅哥都可以娶到自己喜欢的美女,且娶完这个美女后,不会影响到其他人 为什么呢? 假设xi为帅哥,yi和yj为美女,假设给定的完美

POJ 1904 King&#39;s Quest 强连通分量+二分图增广判定

http://www.cnblogs.com/zxndgv/archive/2011/08/06/2129333.html 这位神说的很好 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <string> #include <stack> #include <ve

POJ 1904 King&#39;s Quest(强连通)

POJ 1904 King's Quest 题目链接 题意:n个男人,每个人都有一个喜欢的女人列表,现在给一个完美匹配,问所有完美匹配中,每个人可能娶到的女人列表 思路:强连通,建图,男的连一条边指向女,然后完美匹配的边女的指向男,然后求强连通,在同一个强连通分支并且是自己想娶的的就可能娶到 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> #inc