您的位置:首页 > 其它

POJ 1364 King (差分约束系统)

2016-08-23 23:53 369 查看
题意大致是:给你一些关于一个数列的条件,问你这样的数列是否存在。比如a[k]+...a[k+ni-1]>x
或者 a[k]+...a[k+ni-1]<x

思路:直接表达成前缀和,这样就会出现形如
d(v)<=d(u)+w(u,v)的形式,然后去跑BellmanFord。(因为只要判断有没有负圈,所以不用超级源点)

#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<stdio.h>
#include<vector>
using namespace std;

int n,m;

struct Edge
{
int from;
int to;
int w;
};
vector<Edge> E;
char ope[120];
int dis[120];

void init()
{
E.clear();
}

bool Bellman_ford()
{
memset(dis,0,sizeof(dis));
int size=E.size();
for(int i=0;i<=n;i++)
{
for(int j=0;j<size;j++)
{
int from=E[j].from;
int to=E[j].to;
int w=E[j].w;
if(dis[to]>dis[from]+w)
{
dis[to]=dis[from]+w;
}
}
}

for(int j=0;j<size;j++)
{
int from=E[j].from;
int to=E[j].to;
int w=E[j].w;
if(dis[to]>dis[from]+w)
{
return false;
}
}

return true;
}

int main()
{
while(cin>>n)
{
if(n==0) break;
cin>>m;
init();
for(int i=0;i<m;i++)
{
int a,b,x;
scanf("%d %d",&a,&b);
scanf("%s",ope);
scanf("%d",&x);
if(strcmp(ope,"lt")==0)
{
Edge temp;
temp.from=a-1;
temp.to=a+b;
temp.w=x-1;

E.push_back(temp);
}
else
{
Edge temp;
temp.from=a+b;
temp.to=a-1;
temp.w=-(1+x);
E.push_back(temp);
}
}

bool has=Bellman_ford();
if(has==false)
{
printf("successful conspiracy\n");
}
else printf("lamentable kingdom\n");
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: