Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 12687 | Accepted: 4598 |
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
Source
題目大意:n個數的一個序列,m個約數,si, ni, oi, ki, 代表了序列中第si個數到第si+ni個數的和大於或小於ki,gt = 大於,lt = 小於
問是否存在相悖的約束
約數已經很明顯了設sum[i]爲前i個數的和,那麼就可以得到約束:si, ni, oi, ki
sum[0] = 0
oi爲gt時:sum[si+ni] - sum[si-1] >= ki+1 //差分約束的條件是>= 或者<=所以加一或減一
oi爲lt時:sum[si+ni] - sum[si-1] <= ki-1
求0到其它點的最長路,判斷是否存在環
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define inf 0x3f3f3f3f
struct node
{
int u,v,w;
} eg[500];
int n,m,dist[500];
bool bellman()
{
int i,j;
memset(dist,-inf,sizeof(dist));
//dist[0]=0;
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
int u=eg[j].u;
int v=eg[j].v;
int w=eg[j].w;
if(dist[v]<dist[u]+w)
{
dist[v]=dist[u]+w;
}
}
}
for(j=0; j<m; j++)
{
int u=eg[j].u;
int v=eg[j].v;
int w=eg[j].w;
if(dist[v]<dist[u]+w)
{
return false;
}
}
return true;
}
int main()
{
int i,j;
int a,b,c,d;
char s[5];
while(scanf("%d",&n)&&n)
{
scanf("%d",&m);
for(i=0; i<m; i++)//差分約束的條件是<= 或者 >=
{
scanf("%d%d%s%d",&a,&b,s,&c);
if(strcmp(s,"gt")==0)
{
//add(b-1,a+b,c);
eg[i].u=a-1;
eg[i].v=a+b;
eg[i].w=1+c;
}
if(strcmp(s,"lt")==0)
{
//add(a+b,b-1,-c);
eg[i].u=a+b;
eg[i].v=a-1;
eg[i].w=1-c;
}
}
if(!bellman())
{
printf("successful conspiracy\n");
}
else
{
printf("lamentable kingdom\n");
}
}
return 0;
}