本题构造s[x]=a[1]+a[2]+a[3]+…+a[x]
则a[i]+…+a[i+n]=s[i+n]-s[i-1]
此后用bellman-ford
需要构造超级源点
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<limits.h>
struct edge{
int u,v,w;
}edges[500001];
char ch[100];
int dist[1001];
int main()
{
int n,m;
int u,v;
int s[101],k[101],nn[101];
scanf("%d",&n);
while(n!=0){
scanf("%d",&m);
for(int i=1;i<=m;i++){
scanf("%d%d",&s[i],&nn[i]);
scanf("%s",&ch);
if (ch[0]=='g'){
edges[i].v=s[i]-1;
edges[i].u=s[i]+nn[i];
scanf("%d",&edges[i].w);
edges[i].w=-edges[i].w-1;
}
if (ch[0]=='l'){
edges[i].v=s[i]+nn[i];
edges[i].u=s[i]-1;
scanf("%d",&edges[i].w);
edges[i].w=edges[i].w-1;
}
}
bool flag=true;
for (int i=0;i<=n;i++) dist[i]=0;
for (int i=0;i<=n+1;i++){
for (int j=1;j<=m;j++){
if (dist[edges[j].u]!=INT_MAX &&dist[edges[j].v]>dist[edges[j].u]+edges[j].w)
dist[edges[j].v]=dist[edges[j].u]+edges[j].w;
}
}
for (int j=1;j<=m;j++){
if (dist[edges[j].u]!=INT_MAX &&dist[edges[j].v]>dist[edges[j].u]+edges[j].w) {
flag=false;
break;
}
}
if (!flag) printf("successful conspiracy\n");
else printf("lamentable kingdom\n");
scanf("%d",&n);
}
return 0;
}