# 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

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

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

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

AC代码：

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
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;
}
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;
{
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;
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;
}
int tt=SPFA();
//int tt=Bellman();
if(tt==1)puts("lamentable kingdom");
else puts("successful conspiracy");
}
}


• 本文已收录于以下专栏：

## POJ1364 King(差分约束 SPFA)

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

poj1364

## POJ 1364 King（差分约束系统）

• Tsaid
• 2011-10-14 01:00
• 445

## POJ1364 King 【差分约束】

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

## poj 1364 King[差分约束]

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)