POJ - 1364 (差分约束)
题意:
给出一个数字序列 S={a1,a2,…an},它有 m 个子序列 Si={a[si], a[si+1], a[si+2], … a[si+ni]},现在给出 m 个限制条件:第 i 个子序列的和 < ki 或 第 i 个子序列的和 > ki
思路:
标准的差分约束,用 Si表示 a1+a2...+ai的值,并且 s0=0,所有 Si满足 Si>=0 ,然后再加上题中的限制条件就可以建图了,用bellman求最短路判断是否有负环就可以。
代码:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdlib>
//#include<bits/stdc++.h>
#define mset(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
const int inf=0x3f3f3f3f;
int dis[110];
struct Node{
int u,v,w;
}edge[110];
bool bellman(int x,int n,int m)
{
mset(dis,inf);
dis[x]=0;
int times=0;
bool update=true;
while(update&×<n)
{
update=false;
times++;
for(int i=0;i<m;++i){
int u=edge[i].u,v=edge[i].v,w=edge[i].w;
if(dis[v]>dis[u]+w){
update=true;
dis[v]=dis[u]+w;
}
}
}
if(update){
return false;
}
else
return true;
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);
int n,m;
while(cin>>n,n)
{
cin>>m;
int ind,ls,k;
string op;
for(int i=0;i<m;++i){
cin>>ind>>ls>>op>>k;
if(op=="lt"){
edge[i].u=ind-1;
edge[i].v=ind+ls;
edge[i].w=k-1;
}
else{
edge[i].u=ind+ls;
edge[i].v=ind-1;
edge[i].w=-k-1;
}
}
if(bellman(0,n+1,m)){
cout<<"lamentable kingdom"<<endl;
}
else
cout<<"successful conspiracy"<<endl;
}
return 0;
}