King
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 14791   Accepted: 5226

Description

Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen prayed: ``If my child was a son and if only he was a sound king.'' After nine months her child was born, and indeed, she gave birth to a nice son.
Unfortunately, as it used to happen in royal families, the son was a little retarded. After many years of study he was able just to add integer numbers and to compare whether the result is greater or less than a given integer number. In addition, the numbers had to be written in a sequence and he was able to sum just continuous subsequences of the sequence.

The old king was very unhappy of his son. But he was ready to make everything to enable his son to govern the kingdom after his death. With regards to his son's skills he decided that every problem the king had to decide about had to be presented in a form of a finite sequence of integer numbers and the decision about it would be done by stating an integer constraint (i.e. an upper or lower limit) for the sum of that sequence. In this way there was at least some hope that his son would be able to make some decisions.

After the old king died, the young king began to reign. But very soon, a lot of people became very unsatisfied with his decisions and decided to dethrone him. They tried to do it by proving that his decisions were wrong.

Therefore some conspirators presented to the young king a set of problems that he had to decide about. The set of problems was in the form of subsequences Si = {aSi, aSi+1, ..., aSi+ni} of a sequence S = {a1, a2, ..., an}. The king thought a minute and then decided, i.e. he set for the sum aSi + aSi+1 + ... + aSi+ni of each subsequence Si an integer constraint ki (i.e. aSi + aSi+1 + ... + aSi+ni < ki or aSi + aSi+1 + ... + aSi+ni > ki resp.) and declared these constraints as his decisions.

After a while he realized that some of his decisions were wrong. He could not revoke the declared constraints but trying to save himself he decided to fake the sequence that he was given. He ordered to his advisors to find such a sequence S that would satisfy the constraints he set. Help the advisors of the king and write a program that decides whether such a sequence exists or not.

Input

The input consists of blocks of lines. Each block except the last corresponds to one set of problems and king's decisions about them. In the first line of the block there are integers n, and m where 0 < n <= 100 is length of the sequence S and 0 < m <= 100 is the number of subsequences Si. Next m lines contain particular decisions coded in the form of quadruples si, ni, oi, ki, where oi represents operator > (coded as gt) or operator < (coded as lt) respectively. The symbols si, ni and ki have the meaning described above. The last block consists of just one line containing 0.

Output

The output contains the lines corresponding to the blocks in the input. A line contains text successful conspiracy when such a sequence does not exist. Otherwise it contains text lamentable kingdom. There is no line in the output corresponding to the last ``null'' block of the input.

Sample Input

4 2
1 2 gt 0
2 2 lt 2
1 2
1 0 gt 0
1 0 lt 0
0

Sample Output

lamentable kingdom
successful conspiracy

Source

题目意思:
问你是否存在一个序列S{a1,a2,a3.....an}
可以满足下面两种不同数量的约束
假设s[x]表示a1+....+ax的和
约束1:x y gt w 比如1 2 gt w
从a1开始累加,再加2个的和大于w
根据题目意思即a1+a2+a3>w
变形一下即s[3]-s[0]>w
移动位置变形一下:s[0]-s[3]<-w
继续变形:s[0]-s[3]<=-w-1
即通式为:s[x-1]-s[x+y]<=-w-1
约束2:x y lt w 比如2 2 lt w
从a2开始累加,再加两个的和小于w
即a2+a3+a4<w
变形一下:s[4]-s[1]<w
继续变形:s[4]-s[1]<=w-1
通式:s[x+y]-s[x-1]<=w-1
都是形如x[i]-x[j]<=c的形式
从j指向i 权值为c这样建图
注意:建图完毕之后存在n+1个点,然后在加一个超级源点s,让s到这n+1个点的距离都为0
这样是为了保证图的连通性
然后判断一下图中是否存在负环,存在负环则表示某些约束不能满足
则不存在这样的序列
加了超级源点之后图中一共有n+2个点!!!
建议spfa判负环
 
code:
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
#define INF 9999999999
#define me(a,x) memset(a,x,sizeof(a))
int mon1[]= {,,,,,,,,,,,,};
int mon2[]= {,,,,,,,,,,,,};
int dir[][]= {{,},{,-},{,},{-,}}; int getval()
{
int ret();
char c;
while((c=getchar())==' '||c=='\n'||c=='\r');
ret=c-'';
while((c=getchar())!=' '&&c!='\n'&&c!='\r')
ret=ret*+c-'';
return ret;
}
void out(int a)
{
if(a>)
out(a/);
putchar(a%+'');
} #define max_v 1005
struct node
{
int v;
LL w;
node(int vv=,LL ww=):v(vv),w(ww) {}
};
LL dis[max_v];
int vis[max_v];
int cnt[max_v];
vector<node> G[max_v];
queue<int> q; void init()
{
for(int i=; i<max_v; i++)
{
G[i].clear();
dis[i]=INF;
vis[i]=;
cnt[i]=;
}
while(!q.empty())
q.pop();
} int spfa(int s,int n)
{
vis[s]=;
dis[s]=;
q.push(s);
cnt[s]++; while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=; for(int j=; j<G[u].size(); j++)
{
int v=G[u][j].v;
LL w=G[u][j].w; if(dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
if(vis[v]==)
{
q.push(v);
cnt[v]++;
vis[v]=; if(cnt[v]>n)
return ;
}
}
}
}
return ;
}
int f(int u,int v)
{
for(int j=; j<G[u].size(); j++)
{
if(G[u][j].v==v)
return ;
}
return ;
}
int main()
{
int n,m;
char str[];
int x,y,w;
while(~scanf("%d",&n))
{
if(n==)
break;
scanf("%d",&m);
init();
while(m--)
{
scanf("%d %d %s %d",&x,&y,str,&w);
if(strcmp(str,"gt")==)
{
int u=x+y;
int v=x-;
if(f(u,v))
G[u].push_back(node(v,-w-));
}else if(strcmp(str,"lt")==)
{
int u=x+y;
int v=x-;
if(f(v,u))
G[v].push_back(node(u,w-));
}
}
int s=n+;//超级源点 保证图的连通性
for(int i=;i<=n;i++)//超级源点到每个点的距离为0
{
if(f(s,i))
G[s].push_back(node(i,));
}
int flag=spfa(s,n+);
if(flag==)
printf("lamentable kingdom\n");
else
printf("successful conspiracy\n");
}
return ;
}
/*
题目意思:
问你是否存在一个序列S{a1,a2,a3.....an}
可以满足下面两种不同数量的约束 假设s[x]表示a1+....+ax的和 约束1:x y gt w 比如1 2 gt w
从a1开始累加,再加2个的和大于w
根据题目意思即a1+a2+a3>w
变形一下即s[3]-s[0]>w
移动位置变形一下:s[0]-s[3]<-w
继续变形:s[0]-s[3]<=-w-1
即通式为:s[x-1]-s[x+y]<=-w-1 约束2:x y lt w 比如2 2 lt w
从a2开始累加,再加两个的和小于w
即a2+a3+a4<w
变形一下:s[4]-s[1]<w
继续变形:s[4]-s[1]<=w-1
通式:s[x+y]-s[x-1]<=w-1 都是形如x[i]-x[j]<=c的形式
从j指向i 权值为c这样建图 注意:建图完毕之后存在n+1个点,然后在加一个超级源点s,让s到这n+1个点的距离都为0
这样是为了保证图的连通性
然后判断一下图中是否存在负环,存在负环则表示某些约束不能满足
则不存在这样的序列 加了超级源点之后图中一共有n+2个点!!! 建议spfa判负环
*/

poj 1364 King(线性差分约束+超级源点+spfa判负环)的更多相关文章

  1. POJ 1364 King (差分约束)

    King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8660   Accepted: 3263 Description ...

  2. poj 1364 King(差分约束)

    题意(真坑):傻国王只会求和,以及比较大小.阴谋家们想推翻他,于是想坑他,上交了一串长度为n的序列a[1],a[2]...a[n],国王作出m条形如(a[si]+a[si+1]+...+a[si+ni ...

  3. POJ 3259 Wormholes(SPFA判负环)

    题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...

  4. POJ 1860——Currency Exchange——————【最短路、SPFA判正环】

    Currency Exchange Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u S ...

  5. poj 2049(二分+spfa判负环)

    poj 2049(二分+spfa判负环) 给你一堆字符串,若字符串x的后两个字符和y的前两个字符相连,那么x可向y连边.问字符串环的平均最小值是多少.1 ≤ n ≤ 100000,有多组数据. 首先根 ...

  6. POJ——1364King(差分约束SPFA判负环+前向星)

    King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11946   Accepted: 4365 Description ...

  7. BZOJ.4500.矩阵(差分约束 SPFA判负环 / 带权并查集)

    BZOJ 差分约束: 我是谁,差分约束是啥,这是哪 太真实了= = 插个广告:这里有差分约束详解. 记\(r_i\)为第\(i\)行整体加了多少的权值,\(c_i\)为第\(i\)列整体加了多少权值, ...

  8. poj 3621 二分+spfa判负环

    http://poj.org/problem?id=3621 求一个环的{点权和}除以{边权和},使得那个环在所有环中{点权和}除以{边权和}最大. 0/1整数划分问题 令在一个环里,点权为v[i], ...

  9. POJ 3621 Sightseeing Cows 【01分数规划+spfa判正环】

    题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total ...

随机推荐

  1. linux系统编程:自己动手写一个cp命令

    cp命令的基本用法: cp 源文件 目标文件 如果目标文件不存在 就创建, 如果存在就覆盖 实现一个cp命令其实就是读写文件的操作: 对于源文件: 把内容全部读取到缓存中,用到的函数read 对于目标 ...

  2. CSS计数器(序列数字字符自动递增)详解———张鑫旭

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=4303 一.挖坟不可耻 ...

  3. java的XML解析(DOM4J技术)

    DOM4J技术解析XML文件 一,XML简介 xml (可扩展标记语言)  全称: Extended Markup Language 可扩展的含义:允许程序员按照自己的想法去扩展新的标签 注意:但是扩 ...

  4. BZOJ3108 [cqoi2013]图的逆变换

    Description 定义一个图的变换:对于一个有向图\(G=(V, E)\),建立一个新的有向图: \(V'=\{v_e|e \in E\}\),\(E'=\{(v_b, v_e)|b=(u,v) ...

  5. js-JavaScript实现数字的千位分隔符

    function thousandSeparator(num) { return num && (num .toString().indexOf('.') != -1 ? num.to ...

  6. 解决ubuntu 16.04+ Qt 5.7.1无法输入中文的问题

    解决方法: 1.命令行安装fcitx-frontend-qt5 sudo apt-get install fcitx-frontend-qt5 结果显示如下图,说明我的fcitx-frontend-q ...

  7. [原创]数据驱动决策:BI在零售业的数据化管理

    无论是商业智能时代的应用建设,还是当下大数据时代的数据应用/数据产品建设,行业化.角色化与场景化,均是一个重要的趋势. 当下,许多企业逐步开始具备场景化思维,更为注重用户体验,业务运营更多的围绕用户的 ...

  8. 学习MVC之租房网站(十二)-缓存和静态页面

    在上一篇<学习MVC之租房网站(十一)-定时任务和云存储>学习了Quartz的使用.发邮件,并将通过UEditor上传的图片保存到云存储.在项目的最后,再学习优化网站性能的一些技术:缓存和 ...

  9. linux 查找匹配文件中包含指定字符的 前五行,这里是指所有匹配的前五行

    最近被问到 一个关于查找匹配字符的信息显示问题: 系统/etc/sysctl.conf文件会定义系统内核的一些配置,请查找和net有关的信息,并只打印前面5行信息. 解决方式大概试两种写法均可: 1. ...

  10. Windows桌面.exe程序安装、卸载、升级测试用例

    一.安装 1) 系统:XP.win 7.win 8.win 10 2)安全类型软件:360杀毒.360安全卫士.金山毒霸.百度杀毒.腾讯电脑管家等. 3)同类型软件兼容 4)用户名称:中文用户.英文用 ...