POJ 1364 King【差分约束+SPFA/Bellman-Ford】

King

Time Limit: 1000MS

 

Memory Limit: 10000K

Total Submissions: 11846

 

Accepted: 4325

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

 

题目大意:本题的一是一个国王通过一个序列来做决定,他会有这序列的一段连续子序列,并知道这个子序列的和是大于或者小于某个整数。这里一共有两个信息:

《POJ 1364 King【差分约束+SPFA/Bellman-Ford】》

给你一堆这样的信息,求是否有解。

思路:

1、根据信息,看到了不等式的条件约束,我们不难想到差分约束系统转换为最短路求解。我们还知道,差分约束系统转化为最短路问题的前提是有这样的一个式子成立我们才能够构建边和图:t1-t2<=k

2、所以我们就尽量将已知条件转化为目标形式。我们这里不妨设b=S1+S2+S3+…..Sx+y,设a=S1+S2+S3+….+SX-1,那么我们对已知条件就可以转化为:

b-a>k

b-a<k

这个时候我们还差一个等号,但是因为题中保证了数据都是整数,所以我们再进行一次小小的变化就得到了目标式子:

b-a>=k+1

b-a<=k-1

最后规划式子都变成小于等于,最终得到推导式子:

a-b<=-k-1;

b-a<=k-1

3、既然我们得到了建边建图的条件,然后我们就剩下判断差分约束系统是否有解的问题了,有解,其实就是有向图不存在负权回路,没有解,其实就是有向图存在负权回路,这个问题转化到了最短路上边,我们也就非常好处理了。

4、因为图不一定是连通的,所以确定起点的问题比较尴尬,如果随便控制一个变量是起点,那么会有负权回路判断不到的地方,所以我们这里可以创立一个超级源点,使得一个图连通起来即可。

如果您选择SPFA算法来判断是否存在负权回路:

5、创立超级源点其实相当于第一次就直接将所有点都入队中,就相当于所有点全部连通起来了,这里注意,因为第一次所有点都入队列中了,所以判断是否存在负权回路的条件是这样的:如果某个点出队次数大于n+1,那么就存在负权回路。

如果您选择Bellman算法来判断是否存在负权回路:

6、创立超级源点,然后进行n次遍历,之后再进行一次遍历,如果这次遍历还可以松弛,那么说明存在负权回路。

这里提供两种做法的AC代码,我归到一份代码里边了,然后SPFA算法没有建立超级源点,直接将所有点入队了,Bellman算法建立了超级源点,读者喜欢哪一种方法,就可以对应参考一下。

AC代码:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int head[100000];
struct EdgeNode
{
    int to;
    int w;
    int next;
} e[100000];
int dis[100000];
int vis[100000];
int out[100000];
int cont;
int n,m;
void add(int from,int to,int w)
{
    e[cont].to=to;
    e[cont].w=w;
    e[cont].next=head[from];
    head[from]=cont++;
}
int SPFA()
{
    queue<int >s;
    memset(dis,0,sizeof(dis));
    memset(out,0,sizeof(out));
    for(int i=0;i<=n;i++)
    {
        s.push(i);
    }
    while(!s.empty())
    {
        int u=s.front();
        s.pop();
        vis[u]=0;
        out[u]++;
        if(out[u]>n+1)return 0;
        for(int k=head[u];k!=-1;k=e[k].next)
        {
            int v=e[k].to;int w=e[k].w;
            if(dis[v]>dis[u]+w)
            {
                dis[v]=dis[u]+w;
                if(vis[v]==0)
                {
                    vis[v]=1;
                    s.push(v);
                }
            }
        }
    }
    return 1;
}
int Bellman()
{
    for(int i=0; i<=n; i++)add(n+1,i,0);
    for(int i=0; i<=n; i++)dis[i]=0x3f3f3f3f;
    dis[n+1]=0;
    for(int ci=0; ci<n; ci++)
    {
        for(int i=0; i<=n; i++)
        {
            for(int k=head[i]; k!=-1; k=e[k].next)
            {
                int to=e[k].to;
                int w=e[k].w;
                if(dis[to]>dis[i]+w)
                {
                    dis[to]=dis[i]+w;
                }
            }
        }
    }
    for(int i=0; i<=n; i++)
    {
        for(int k=head[i]; k!=-1; k=e[k].next)
        {
            int to=e[k].to;
            int w=e[k].w;
            if(dis[to]>dis[i]+w)
            {
                return 0;
                dis[to]=dis[i]+w;
            }
        }
    }
    return 1;
}
int main()
{
    while(~scanf("%d",&n))
    {
        if(n==0)break;
        cont=0;
        memset(head,-1,sizeof(head));
        scanf("%d",&m);
        for(int i=0; i<m; i++)
        {
            int x,y,k;
            char op[5];
            scanf("%d%d%s%d",&x,&y,&op,&k);
            int b=x+y;
            int a=x-1;
            if(op[0]=='g')add(b,a,-k-1);
            else add(a,b,k-1);
        }
        int tt=SPFA();
        //int tt=Bellman();
        if(tt==1)puts("lamentable kingdom");
        else puts("successful conspiracy");
    }
}

    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/mengxiang000000/article/details/51557071
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞