POJ1364 King 【差分约束】

King

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9977   Accepted: 3711

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

题意:参考http://blog.csdn.net/wangjian8006/article/details/7956848题解:将题目所给的条件转化成Xi - Xj <= W;的形式,然后建立一个虚拟节点0,该节点到每个顶点的距离为0,再用SPFA判断该图是否存在负环。

#include <stdio.h>
#include <string.h>
#include <queue>
#define maxn 106
#define inf 0x3f3f3f3f
using std::queue;

int head[maxn], out[maxn];
struct Node{
	int to, w, next;
} E[maxn << 1];
int dist[maxn];
bool vis[maxn];

void addEdge(int a, int b, int c, int i)
{
	E[i].to = b; E[i].w = c;
	E[i].next = head[a]; head[a] = i;
}

bool SPFA(int s, int n)
{
	int i, u, v, tmp;
	for(i = 0; i <= n + 1; ++i){
		dist[i] = inf; out[i] = vis[i] = 0;
	}
	queue<int> Q; Q.push(s);
	vis[s] = 1; dist[s] = 0;
	while(!Q.empty()){
		u = Q.front(); Q.pop();
		if(++out[u] > n) return false;
		for(i = head[u]; i != -1; i = E[i].next){
			tmp = dist[u] + E[i].w;
			v = E[i].to;
			if(tmp < dist[v]){
				dist[v] = tmp;
				if(!vis[v]) Q.push(v);
			}
		}
	}
	return true;
}

int main()
{
	int n, m, i, a, b, c;
	char str[5];
	while(scanf("%d%d", &n, &m) == 2){
		memset(head, -1, sizeof(head));
		for(i = 0; i < m; ++i){
			scanf("%d%d%s%d", &a, &b, str, &c);
			if(str[0] == 'g') addEdge(a+b+1, a, -c-1, i);
			else addEdge(a, b+a+1, c - 1, i);
		}
		for(i = 1; i <= n + 1; ++i)
			addEdge(0, i, 0, m + i - 1);
		if(SPFA(0, n + 1)) printf("lamentable kingdom\n");
		else printf("successful conspiracy\n");
	}
	return 0;
}

POJ1364 King 【差分约束】

时间: 2024-08-26 23:40:15

POJ1364 King 【差分约束】的相关文章

poj1364 King --- 差分约束

这是我见过最扯淡的题面之一. 题读了差不多一半我都觉得我这题肯定读不懂了,到最后终于看到重点了靠! 就是个差分约束大水题!毫无新意! 扯些什么皇后想生孩子!生了男孩是个弱智!父王很担心!这些有的没的有意思吗!! 题目就是给一个序列,告诉你 a b gt/lt c 表示从a起的b+1个数之和大于/小于c 就根据这个列不等式,要把> 或 <关系换成>= <= 就减一就可以了 列出不等式: S[a-1]-S[a+b]<=-c-1 S[a+b]-S[a-1]<=c-1 需要注意

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

UVALive 5532 King(差分约束,spfa)

题意:假设一个序列S有n个元素,现在有一堆约束,限制在某些连续子序列之和上,分别有符号>和<.问序列S是否存在?(看题意都看了半小时了!) 注意所给的形式是(a,b,c,d),表示:区间之和:sum[a,a+b]<d或者sum[a,a+b]>d.而c是两个字符构成,判断前1个字符足矣. 思路: 首先考虑要用点来表示什么,可以看到所给的是区间,也就是首尾的位置,可令sum(a)表示序列a[1...a]的和,那么表达式大概为sum(a+b)-sum(a-1)<k,由于小于号的存在

hdu 差分约束题集

[HDU]1384 Intervals 基础差分约束★1529 Cashier Employment 神级差分约束★★★★ 1531 King 差分约束★1534 Schedule Problem 差分约束输出一组解★3440 House Man 比较好的差分约束★★3592 World Exhibition 简单★3666 THE MATRIX PROBLEM 中等★★4274 Spy's Work [先处理出欧拉序列,然后就是差分约束了...] [POJ]1201 Intervals1275

差分约束小结

ZOJ 2770 Burn the Linked Camp /* ZOJ 2770 Burn the Linked Camp 差分约束 */ #include <iostream> #include <cstring> #include <cstdio> #include <queue> using namespace std; const int MAXN = 1009; struct Edge { int v, ne, c; } G[MAXN*MAXN]

【差分约束】POJ1364/LG UVA515 king

这道题是个差分约束 King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14901 Accepted: 5248 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 sou

SGU 298 King Berl VI 差分约束 并使得min(dis[n]-dis[1])

题目链接:点击打开链接 题意: 给定n个点m条约束. 下面输出 u v x 表示: dis[u] - dis[v] >= x 然后建图就是 u->v 边权为-x 输出一个解满足 -10000<= dis[i] <= 10000. 若有多解输出一个 dis[n] - dis[1] 最小的解. 思路: 正图求个最大解,反图求个最小解. 对于一个点的 最大解<最小解,则差分约束无解. 题目要求dis[n]-dis[1]最小,那就令dis[n]为其最小解,dis[1]为其最大解,再s