#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#include<queue>
#include<vector>
#include<set>
#include<climits>
#include<map>
#include<string>
using namespace std;
const int INF=100000000;
const int maxn=100+10;
const int maxm=100*100+10;
int n,m;
int first[maxn];
int u[maxm],v[maxm],nnext[maxm],w[maxm];
int V,E;
int d[maxn];
bool Bellman_Ford()
{
queue<int> q;
bool inq[maxn];
int c[maxn];
V=0;
for(int i=0;i<=n+1;i++)d[i]=(i==n+1 ? 0:INF);
memset(inq,0,sizeof(inq));
memset(c,0,sizeof(c));
q.push(n+1);
while(!q.empty())
{
int x=q.front();
q.pop();
inq[x]=false;
for(int e=first[x];e!=-1;e=nnext[e])if(d[v[e]]>d[x]+w[e])
{
d[v[e]]=d[x]+w[e];
if(!inq[v[e]])
{
inq[v[e]]=true;
q.push(v[e]);
c[v[e]]++;
if(c[v[e]]==n+2)return true;
}
}
}
return false;
}
int main()
{
while(~scanf("%d",&n))
{
if(n==0)break;
scanf("%d",&m);
for(int i=0;i<=n+1;i++)
first[i]=-1;
E=0;
for(int i=0;i<m;i++)
{
char op[10];
int si,ni,ki;
scanf("%d %d %s %d",&si,&ni,op,&ki);
if(op[0]=='g')
{
u[E]=si+ni;
v[E]=si-1;
w[E]=-ki-1;
nnext[E]=first[u[E]];
first[u[E]]=E;
E++;
}
else
{
u[E]=si-1;
v[E]=si+ni;
w[E]=ki-1;
nnext[E]=first[u[E]];
first[u[E]]=E;
E++;
}
}
for(int i=0;i<n+1;i++)
{
u[E]=n+1;
v[E]=i;
w[E]=0;
nnext[E]=first[u[E]];
first[u[E]]=E;
E++;
}
if(Bellman_Ford())printf("successful conspiracy\n");
else printf("lamentable kingdom\n");
}
return 0;
}
UVa 515 King (差分约束+线性规划+判负圈)
最新推荐文章于 2024-05-29 13:37:57 发布