Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 12493 | Accepted: 4533 |
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
这个题就比较简单了,对于差分系统来说。
大意:老国王去世了,小国王比较笨,然后有些人想要谋权篡位,这些人给新国王出了一些题目,让国王作出决策。问题是从序列S={a1,a2,.....an}中取出一个子序列Si={asi,asi+1,.......asi+ni}国王有一个思考时间,然后必须做出判断:他必须对每个给出的子序列中的每个数进行加和,然后对每个子序列的和设定一个约束ki,即asi+asi+1...asi+ni<ki或者是>ki。
过了一会,他意识到他的判断是错误的。他不能取消他设定的约束,但他努力挽救自己:通过伪造篡位者给他的整数序列。然后问你这样的序列存不存在。
存在输出:lamentable kingdom不存在输出successful conspiracy。
思路:把限制条件建模成差分约束系统,最终就是判断有没有负环,有就不存在序列,反之存在。
限制条件的转化:s[i]表示序列前i的数的和,那么则有:
s[i+n]-s[i-1]>d
s[i+n]-s[i-1]<d
转化为:
s[i-1]-s[i+n]<=-d-1;
s[i+n]-s[i-1]<=d-1;
然后建图求解就行啦,注意一开始要全部的点进队列来判断负环。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXN=100+10;
const int inf=1e9;
int head[MAXN],dis[MAXN],sum[MAXN];
bool vis[MAXN];
int n,m;
int cnt;
void init()
{
cnt=0;
for(int i=0;i<=n;++i)
{
head[i]=-1;
dis[i]=inf;
vis[i]=0;
sum[i]=1;
}
}
struct node
{
int v;
int w;
int next;
}e[MAXN];
void add(int u,int v,int w)
{
e[cnt].v=v;
e[cnt].w=w;
e[cnt].next=head[u];
head[u]=cnt++;
}
bool spfa()
{
queue<int>q;
for(int i=0;i<=n;++i)
{
q.push(i);
vis[i]=1;
}
while(!q.empty())
{
int u=q.front();
vis[u]=0;
q.pop();
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].v;
int w=e[i].w;
int t=dis[u]+w;
if(dis[v]>t)
{
dis[v]=t;
if(!vis[v])
{
sum[v]++;
if(sum[v]>n+1)return 0;
vis[v]=1;
q.push(v);
}
}
}
}
return 1;
}
int main()
{
char s[10];
while(~scanf("%d",&n)&&n)
{
scanf("%d",&m);
int u,v,d;
init();
while(m--)
{
scanf("%d%d%s%d",&u,&v,s,&d);
if(s[0]=='g')
{
add(u+v,u-1,-d-1);
}
else
{
add(u-1,u+v,d-1);
}
}
if(spfa())puts("lamentable kingdom");
else puts("successful conspiracy");
}
return 0;
}