这是我见过最扯淡的题面之一。
题读了差不多一半我都觉得我这题肯定读不懂了,到最后终于看到重点了靠!
就是个差分约束大水题!毫无新意!
扯些什么皇后想生孩子!生了男孩是个弱智!父王很担心!这些有的没的有意思吗!!
题目就是给一个序列,告诉你 a b gt/lt c 表示从a起的b+1个数之和大于/小于c
就根据这个列不等式,要把> 或 <关系换成>= <= 就减一就可以了
列出不等式:
S[a-1]-S[a+b]<=-c-1
S[a+b]-S[a-1]<=c-1
需要注意的是,不是每次添加附加结点0都是正确的,要保证添加的点是不会使用到的,比如n+1
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#define inf 0x3f3f3f3f
#define eps 1e-6
#define ll __int64
using namespace std;
struct node
{
int v,w,next;
}e[10010];
int n,m,h,head[110],d[110],inq[110],outq[110];
void init()
{
memset(head,-1,sizeof head);
h=0;
}
void addedge(int a,int b,int c)
{
e[h].v=b;
e[h].w=c;
e[h].next=head[a];
head[a]=h++;
}
int spfa(int st)
{
memset(d,0x3f,sizeof d);
memset(inq,0,sizeof inq);
memset(outq,0,sizeof outq);
// d[st]=0;inq[st]=1;
queue<int> q;
q.push(st);
for(int i=1;i<=n;i++)
{
inq[i]=1;
q.push(i);
}
while(!q.empty())
{
int x=q.front();
q.pop();
inq[x]=0;
outq[x]++;
if(outq[x]>n+1) return 0;//存在负环//是在与源点连通的图上有负环
for(int i=head[x];i!=-1;i=e[i].next)
{
if(d[e[i].v]>d[x]+e[i].w)
{
d[e[i].v]=d[x]+e[i].w;
if(!inq[e[i].v])
{
inq[e[i].v]=1;
q.push(e[i].v);
}
}
}
}
return 1;
}
int main()
{
int a,b,c;
char s[10];
while(scanf("%d",&n)&&n)
{
init();
scanf("%d",&m);
while(m--)
{
scanf("%d%d%s%d",&a,&b,s,&c);
if(s[0]=='g')
addedge(b+a,a-1,-c-1);
else
addedge(a-1,b+a,c-1);
}
// for(int i=0;i<=n;i++)
// addedge(0,i,0);
int ans=spfa(0);
if(ans)
printf("lamentable kingdom\n");
else printf("successful conspiracy\n");
}
return 0;
}
poj1364 King --- 差分约束,布布扣,bubuko.com
原文:http://blog.csdn.net/u011032846/article/details/36368221