首页 > 代码库 > POJ1364 HDU1531 King【SPFA】【差分约束】

POJ1364 HDU1531 King【SPFA】【差分约束】

King
Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 10528Accepted: 3872

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


题目大意:一个国王通过一个序列来做决定,他有几段段连续子序列的和的条件,知道

各段子序列的和是大于还有小于某个数(这个数已知)。问:是否能找到这样的序列。

转换一下,就是N个数组成一个序列,已知M段几个连续的数组成的子序列构成的不等

式,求这几个不等式构成的不等式组是否有解。

例如:序列{A1,A2,A3,A4,A5,…,An},有M个不等式,比如说:

Ai + A(i+1) + A(i+2) + A(i+3) + A(i+4) + … + A(i+t) < Ki 或者是

Ai + A(i+1) + A(i+2) + A(i+3) + A(i+4) + … + A(i+t) > Ki 

题目输入格式说明:

第1行:N M,表示N个数构成一个序列,有M段子段。

接下来1~M行:s n o k,s表示该子段的第一个数是序列的第几个数,即As;n表示从As

开始往后到A(s+n),即子段为As + A(s+1) + A(s+2) + … + A(s+n) ;o是一个字符串,

字符串为gt代表‘>‘,为lt代表‘<‘;最后k表示不等式右边的值。

数据举例说明:

1 2 gt 0  表示:a1 + a2 + a3 > 0

2 2 lt 2  表示: a2 + a3 + a4 < 2

lamentable kingdom最后如果所有不等式均满足条件,则输出"lamentable kingdom",不

满足则输出"successful conspiracy"。

思路:典型的差分约束系统。将所有的不等式转换一下。

设Si为序列前n项和。Si = A1 + A2 + A3 + … + Ai

那么 :

a b gt c 转换为: Sa - S(a+b) <= -c-1

a b lt  c 转换为: S(a+b) - S(a-1) <= c-1

根据差分约束系统,加入这些有向边。之后用SPFA算法求最短路径,并判断有无负环出现。

若有负环出现,则不等式组不成立。若没有负环,则不等式组有解。

参考博文:http://blog.csdn.net/wangjian8006/article/details/7956848


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAXM = 220;
const int MAXN = 110;
const int INF = 0xffffff0;

struct EdgeNode
{
    int to;
    int w;
    int next;
}Edges[MAXM];

int Dist[MAXN],vis[MAXN],Head[MAXN],outque[MAXN];

bool SPFA(int s,int N)
{
    for(int i = 0; i <= N+1; ++i)
        Dist[i] = INF;
    memset(vis,0,sizeof(vis));
    memset(outque,0,sizeof(outque));
    queue<int> Q;
    Q.push(s);
    vis[s] = 1;
    Dist[s] = 0;
    while( !Q.empty())
    {
        int u = Q.front();
        Q.pop();
        outque[u]++;
        if(outque[u] > N)
            return false;
        vis[u] = 0;
        for(int i = Head[u]; i != -1; i = Edges[i].next)
        {
            int Temp = Dist[u] + Edges[i].w;
            if(Temp < Dist[Edges[i].to])
            {
                Dist[Edges[i].to] = Temp;
                if( !vis[Edges[i].to])
                {
                    vis[Edges[i].to] = 1;
                    Q.push(Edges[i].to);
                }
            }
        }
    }
    return true;
}
int main()
{
    int N,M,a,b,c;
    char ch[4];
    while(~scanf("%d",&N) && N)
    {
        scanf("%d",&M);
        memset(Head,-1,sizeof(Head));
        for(int i = 0; i < M; ++i)
        {
            scanf("%d%d%s%d",&a,&b,ch,&c);
            if(ch[0] == 'g')
            {
                int u = a + b + 1;
                int v = a;
                int w = -c-1;
                Edges[i].to = v;
                Edges[i].w = w;
                Edges[i].next = Head[u];
                Head[u] = i;
            }
            else
            {
                int u = a;
                int v = a + b + 1;
                int w = c - 1;
                Edges[i].to = v;
                Edges[i].w = w;
                Edges[i].next = Head[u];
                Head[u] = i;
            }
        }
        for(int i = 1; i <= N+1; ++i)
        {
            int u = 0;
            int v = i;
            int w = 0;
            int k = M+i-1;
            Edges[k].to = v;
            Edges[k].w = w;
            Edges[k].next = Head[u];
            Head[u] = k;
        }

        if(SPFA(0,N+1))
            printf("lamentable kingdom\n");
        else
            printf("successful conspiracy\n");
    }

    return 0;
}


POJ1364 HDU1531 King【SPFA】【差分约束】