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

## Problem:

Time Limit: 1000MS |
Memory Limit: 10000K |

Description

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

Output

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

## 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; }

## 发表评论