题意: 问是否存在长度为n的序列满足m个条件. 每个条件描述某一段子序列和大于或者小于k.
题解:差分约束的应用,用最长路可求,可添加超级源点。SPFA中,如果一个点入队超过n次,那么表明此图存在负权值回路。
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-9
#define maxn 1000010
#define MOD 1000000007
const int N = 110;
struct Edge
{
int to,next,weight;
} edge[4*N];
int n,m,k;
int dis[N],num[N],tot,ans;
int headlist[N],vis[N];
void add_edge(int u,int v,int weight)
{
edge[tot].to = v;
edge[tot].weight = weight;
edge[tot].next = headlist[u];
headlist[u] = tot++;
}
int SPFA(int u)
{
queue<int> q;
memset(dis,-INF,sizeof(dis));
memset(num,0,sizeof(num));
memset(vis,0,sizeof(vis));
q.push(u);
vis[u] = 1;
dis[u] = 0;
num[u] = 1;
while(!q.empty())
{
int x = q.front();
q.pop();
vis[x] = 0;
for(int i = headlist[x]; i != -1; i = edge[i].next)
{
int y = edge[i].to;
if(dis[y] < dis[x] + edge[i].weight)
{
dis[y] = dis[x] + edge[i].weight;
if(!vis[y])
{
q.push(y);
vis[y] = 1;
if(++num[y] > n)
return 0;
}
}
}
}
return 1;
}
int main()
{
int t;
//scanf("%d",&t);
while(scanf("%d",&n) && n)
{
tot = 0;
memset(headlist,-1,sizeof(headlist));
scanf("%d",&m);
for(int i = 0; i < m; i++)
{
int x,y,z;
char s[3];
scanf("%d%d%s%d",&x,&y,s,&z);
if(s[0] == 'g')
add_edge(x-1,x+y,z+1);
else
add_edge(x+y,x-1,-z+1);
}
for(int i = 0; i <= n; i++)
add_edge(n+1,i,0);
if(SPFA(n+1))
printf("lamentable kingdom\n");
else
printf("successful conspiracy\n");
}
return 0;
}