7
10
2015
0

POJ1364:King

                     King
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 10931   Accepted: 4004

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

    今天差分约束的第二题,我觉得相对于第一题(链接:http://wyxoi.is-programmer.com/posts/102283.html)来说较为简单,做法和第一题相同,记一个sum数组为a的前缀和,对一段数si到si+ni的区间内的数之和小于或大于k,可以得到两个不等式:

   sum[si+ni]-sum[si-1]>k || sum[si+ni]-sum[si-1]<k

因为差分约束只能处理小于等于和大于等于的情况而且题目中a的范围为整数所以可以改成

   sum[si+ni]-sum[si-1]>=k+1 || sum[si+ni]-sum[si-1]<=k-1

但是我们可以发现两个式子的不等号方向不同,所以我们需要将其中一个式子进行转化(我的代码中是将第一个式子的大于号改变方向),使两个式子的不等号方向相同,然后就可以用Spfa判断是否符合条件。

 

   细节:因为连边之后的图不一定是联通的,所以我们要建一个虚点连向所有点,这样可以保证所有联通块都被处理过(我的代码中将所有的点先入队列,作用与建一个虚点相同)。

#include <cstdio>
#include <cstring>
#include <algorithm>
#define M 210
#define N 110
using namespace std;
struct data{int x,y,s,next;} e[M];
int cnt,head[N],dis[N],tot[N],v[N],q[N*N],x,y,k,n,m,h,t;
char opt[10];
void Insert(int x,int y,int s){
	e[++cnt].x = x;e[cnt].y = y;e[cnt].s = s;
	e[cnt].next = head[x];head[x] = cnt;
}
int Spfa(){
	for (int i=0;i<=n;i++) dis[i] = 1e9;
	dis[0] = 0;h = 0;
	for (int i=0;i<n;i++) q[++t] = i;//这一步将所有点都先入队列,作用相当于建立一个虚点
	while (h<t){
		int now = q[++h];v[now] = 0;
		for (int i=head[now];i;i=e[i].next)
			if (dis[now]+e[i].s<dis[e[i].y]){
				dis[e[i].y] = dis[now]+e[i].s;
				if (!v[e[i].y]) {
					tot[e[i].y]++;if (tot[e[i].y]>n) return 0;
					v[e[i].y] = 1;q[++t] = e[i].y;
				}
			}
	}
	return 1;
}
int main(){
	scanf("%d",&n);
	while (n){
		memset(head,0,sizeof(head));
		memset(tot,0,sizeof(tot));
		cnt = 0;
		scanf("%d",&m);
		for (int i=1;i<=m;i++){
			scanf("%d %d %s %d",&x,&y,&opt,&k);
			if (opt[0]=='g') Insert(x+y,x-1,-k-1);
			else Insert(x-1,x+y,k-1);
		}
		if (Spfa()) printf("lamentable kingdom\n");
		else printf("successful conspiracy\n");
		scanf("%d",&n);
	}
}

 

Category: POJ | Tags: | Read Count: 453

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com