题目大意:有一个序列。给定一些约束条件,格式为si、ni、oi、ki,意思是序列中第si项到第si+ni项的和>(或<)ki,oi表示>(用"gt”表示)或<(用"lt"表示)。问这样的序列是否存在。存在输出"lamentable kingdomi",否则输出"successful conspiracy"。
思路:简单的差分约束,就是题目描述有点不太好懂,还有就是,题目给出的是>,<,要把它转化成>=,<=。需要注意,虽然序列长度为n,但是建图的时候用来第0项,相当于序列长度变为了n+1,所以bellman算法要循环n次!
#include<queue>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define M 110
#define INF 0x3f3f3f3f
struct node
{
int u, v, w;
}edge[M];
int n, m;
int mx, mn;
int dist[M];
void bellman_ford()
{
memset(dist, 0, sizeof(dist));
for (int i = 1; i <= n; i++)//循环n次!
{
for (int i = 0; i <= m; i++)
{
int a = edge[i].u, b = edge[i].v;
if (dist[a] < INF && dist[b] > dist[a] + edge[i].w)
{
dist[b] = dist[a] + edge[i].w;
}
}
}
}
int main()
{
while (cin >> n, n)
{
cin >> m;
//mx = -INF; mn = INF;
for (int i = 1; i <= m; i++)
{
int a, b, k;
string c;
cin >> a >> b >> c >> k;
if (c == "gt")
{
k++;
edge[i].u = a + b;
edge[i].v = a - 1;
edge[i].w = -k;
}
else
{
k--;
edge[i].u = a - 1;
edge[i].v = a + b;
edge[i].w = k;
}
}
bellman_ford();
int flog = 0;
for (int i = 1; i <= m; i++)
{
int a = edge[i].u, b = edge[i].v;
if (dist[a] < INF && dist[b] > dist[a] + edge[i].w)
{
flog = 1;
break;
}
}
if (flog)
{
printf("successful conspiracy\n");
}
else
printf("lamentable kingdom\n");
}
return 0;
}