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

原创 2016年06月01日 16:51:29


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

 

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


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

思路:

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");
    }
}







版权声明:0.0小白有写的不好不对的地方请指出~

差分约束非常好的题 poj 2983 spfa 为什么不加单源点会错 ,这道题后才是真正懂得差分约束真正怎么作。

开始一直没有检查出来错误,用 spfa ,在网上看别人也有类似的错。分析题型,这是一个可能没有原点 ,的最短路, 可能图不是连通的。spfa 判断差分约束的时候是单源的。比如这组数据6 6V 2 5V...
  • mindmb
  • mindmb
  • 2010年12月08日 09:29
  • 1112

差分约束系统的学习 poj1364(bellman和spfa)

King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13843 Accepted: 4920 Description ...

SPFA算法

SPFA算法 SPFA(Shortest Path Faster Algorithm)(队列优化)算法是求单源最短路径的一种算法,在Bellman-ford算法的基础上加上一个队列优化,减少...

hdu 1142(最短路dijkstra)

假设 A 和 B 是相连的,当前在 A 处,如果 A 到终点的距离大于 B 到终点的距离, 则可以从 A 通往 B 处,问满足这种的条件的路径条数。      1、以终点 2 为起点 dijkstra...

POJ 1364 King 差分约束 Bellman_Ford

模板题,一直想用SPFA来做,可是这题却遇到了麻烦。因为对于原来的题来说一直想弄懂那个超级原点是怎么做的。这次用SPFA弄了N久,还是没弄出来... 后来果断用了BellmanFord()裸A啊......

POJ1364/ZOJ1260 King(差分约束,spfa)

差分的题,建图还是挺纠结的,虽然这个题没有卡建图,所有不等式都给你了,没有隐含条件,只是注意把 )转化成 = )就可以了,但是还是逗留在套模板的那个层面,没有深刻理解其中的理论意义,以后还要继续努力。...

POJ 1364 King 差分约束 SPFA

终于用SPFA AC了~~这题还真是!!稍后要把差分约束和SPFA总结一下~~,觉得这题还是挺有意思的。具体为什么WA了呢??? 不是那么清楚~~ 照着做就是AC了。发现形成链表就那么简单~ 嘿嘿~ ...

POJ1364 King(差分约束 SPFA)

King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11146   Accepted: 4081 ...

POJ1716—差分约束系统+Bellman-Ford算法 || SPFA算法的实现

题目看去有点难下手,如果学习了差分约束系统或是贪心算法会觉得很简单。 刚开始时用Bellman-Ford算法解,采用的是求最短路径,由于Bellman-Ford算法复杂度本身就高,再加上增设了m+1...

poj1364--King(差分约束)

poj1364:题目链接  题目大意:n个数的一个序列,m个约数,si, ni, oi, ki, 代表了序列中第si个数到第si+ni个数的和大于或小于ki,gt = 大于,lt = 小于 问是否...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ 1364 King【差分约束+SPFA/Bellman-Ford】
举报原因:
原因补充:

(最多只允许输入30个字)