[POJ 1364] King【差分约束系统】

  • 2018-01-05
  • 0
  • 0

Problem:

Time Limit: 1000MS Memory Limit: 10000K

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

Solution:

本题是一道很不错的差分约束系统存在性问题。

同样用 S[i] 表示前缀和,并与题意相对应地用 s 表示子串起始位置,s + n 表示终止位置,o 为符号类型,k 表示大于或小于的常数值,则有

  • 若 o 为 gt (>),则 S[s + n] - S[s - 1] > k,化为三角不等式即 S[s - 1] ≤ S[s + n] - k - 1
  • 若 o 为 lt (<),则 S[s + n] - S[s - 1] < k,化为三角不等式即 S[s + n] ≤ S[s - 1] + k - 1

按三角不等式建图跑最短路即可,详见 [POJ 1201] Intervals【差分约束系统】,在此不再赘述。

此题的最短路最好跑 SPFA,但由于数据范围小,一定要跑 Bellman-Ford 也勉强可以,但 Dijkstra 就挂了,因为差分约束系统的存在性取决于图中是否有负权环

【SPFA 判负权环】

在队列元素中记录路径长度,一旦长度超过图中的顶点数说明有负权环。因为若没有负权环,不会重复松弛 (Relax) 该路径上已出现过的点,故路径长度不会超过顶点数。

这里一定要注意,由于记录了前缀和,图中的顶点应为 0 ~ N,共有 N + 1 个。我就因为把它当作了 N 个而 WA 了好多次。

Code: O(M) ~ O(NM) [164K, 0MS]

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

int n, m;

struct Edge{
	int np, val;
	Edge *nxt;
};

struct Graph{
	Edge *V[105], E[105];
	int tope;
	
	inline void clear() {tope = 0, memset(V, 0, sizeof(V));}
	
	inline void addedge(int u, int v, int w){
		E[++tope].np = v, E[tope].val = w;
		E[tope].nxt = V[u], V[u] = &E[tope];
	}
} G;

struct qnode{
	int id, step;
	
	qnode() {}
	qnode(int id, int step): id(id), step(step) {}
};

struct queue{
	#define inc(x) (x) = ((x) == 104 ? 1 : (x) + 1)
	qnode node[105];
	int fr, re;
	
	inline void clear() {fr = re = 0;}
	
	inline bool empty() {return fr == re;}
	
	inline void push(const qnode &x) {node[re] = x, inc(re);}
	
	inline void pop() {inc(fr);}
	
	inline qnode front() {return node[fr];}
} q;

int dis[105];
bool inq[105];

inline bool SPFA_negative_cycle(){
	memset(dis, 0, sizeof(dis)), q.clear();
	for(register int i = 0; i <= n; i++)
		q.push(qnode(i, 1)), inq[i] = 1;
	while(!q.empty()){
		qnode u = q.front();
		q.pop(), inq[u.id] = 0;
		for(register Edge *ne = G.V[u.id]; ne; ne = ne->nxt)
			if(dis[u.id] + ne->val < dis[ne->np]){
				dis[ne->np] = dis[u.id] + ne->val;
				if(u.step == n + 1) return 1;
				// If the number of the points on a path exceeds n + 1, there must be negative cycles
				// Beware that the number of points is n + 1 (identified as 0 ~ n), but not n !!!
				q.push(qnode(ne->np, u.step + 1)), inq[ne->np] = 1;
			}
	}
	return 0;
}

int main(){
	while(scanf("%d", &n) != EOF && n){
		scanf("%d", &m);
		G.clear();
		for(register int i = 1; i <= m; i++){
			int s, n, k;
			char o[5];
			scanf("%d%d%s%d", &s, &n, o, &k);
			if(o[0] == 'g') G.addedge(s + n, s - 1, -k - 1);
			else G.addedge(s - 1, s + n, k - 1);
		}
		if(SPFA_negative_cycle()) puts("successful conspiracy");
		else puts("lamentable kingdom");
	}
	return 0;
}

评论

还没有任何评论,你来说两句吧



常年不在线的QQ:
49750

不定期更新的GitHub:
https://github.com/Darkleafin


OPEN AT 2017.12.10

如遇到代码不能正常显示的情况,请刷新页面。
If the code cannot be displayed normally, please refresh the page.


发现一个优美的网站:
https://visualgo.net/en
















- Theme by Qzhai