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

  1. 4 2
  2. 1 2 gt 0
  3. 2 2 lt 2
  4. 1 2
  5. 1 0 gt 0
  6. 1 0 lt 0
  7. 0

Sample Output

  1. lamentable kingdom
  2. 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:
  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<math.h>
  4. #include<string.h>
  5. #include<set>
  6. #include<map>
  7. #include<list>
  8. #include<math.h>
  9. #include<queue>
  10. #include<algorithm>
  11. using namespace std;
  12. typedef long long LL;
  13. #define INF 9999999999
  14. #define me(a,x) memset(a,x,sizeof(a))
  15. int mon1[]= {,,,,,,,,,,,,};
  16. int mon2[]= {,,,,,,,,,,,,};
  17. int dir[][]= {{,},{,-},{,},{-,}};
  18.  
  19. int getval()
  20. {
  21. int ret();
  22. char c;
  23. while((c=getchar())==' '||c=='\n'||c=='\r');
  24. ret=c-'';
  25. while((c=getchar())!=' '&&c!='\n'&&c!='\r')
  26. ret=ret*+c-'';
  27. return ret;
  28. }
  29. void out(int a)
  30. {
  31. if(a>)
  32. out(a/);
  33. putchar(a%+'');
  34. }
  35.  
  36. #define max_v 1005
  37. struct node
  38. {
  39. int v;
  40. LL w;
  41. node(int vv=,LL ww=):v(vv),w(ww) {}
  42. };
  43. LL dis[max_v];
  44. int vis[max_v];
  45. int cnt[max_v];
  46. vector<node> G[max_v];
  47. queue<int> q;
  48.  
  49. void init()
  50. {
  51. for(int i=; i<max_v; i++)
  52. {
  53. G[i].clear();
  54. dis[i]=INF;
  55. vis[i]=;
  56. cnt[i]=;
  57. }
  58. while(!q.empty())
  59. q.pop();
  60. }
  61.  
  62. int spfa(int s,int n)
  63. {
  64. vis[s]=;
  65. dis[s]=;
  66. q.push(s);
  67. cnt[s]++;
  68.  
  69. while(!q.empty())
  70. {
  71. int u=q.front();
  72. q.pop();
  73. vis[u]=;
  74.  
  75. for(int j=; j<G[u].size(); j++)
  76. {
  77. int v=G[u][j].v;
  78. LL w=G[u][j].w;
  79.  
  80. if(dis[v]>dis[u]+w)
  81. {
  82. dis[v]=dis[u]+w;
  83. if(vis[v]==)
  84. {
  85. q.push(v);
  86. cnt[v]++;
  87. vis[v]=;
  88.  
  89. if(cnt[v]>n)
  90. return ;
  91. }
  92. }
  93. }
  94. }
  95. return ;
  96. }
  97. int f(int u,int v)
  98. {
  99. for(int j=; j<G[u].size(); j++)
  100. {
  101. if(G[u][j].v==v)
  102. return ;
  103. }
  104. return ;
  105. }
  106. int main()
  107. {
  108. int n,m;
  109. char str[];
  110. int x,y,w;
  111. while(~scanf("%d",&n))
  112. {
  113. if(n==)
  114. break;
  115. scanf("%d",&m);
  116. init();
  117. while(m--)
  118. {
  119. scanf("%d %d %s %d",&x,&y,str,&w);
  120. if(strcmp(str,"gt")==)
  121. {
  122. int u=x+y;
  123. int v=x-;
  124. if(f(u,v))
  125. G[u].push_back(node(v,-w-));
  126. }else if(strcmp(str,"lt")==)
  127. {
  128. int u=x+y;
  129. int v=x-;
  130. if(f(v,u))
  131. G[v].push_back(node(u,w-));
  132. }
  133. }
  134. int s=n+;//超级源点 保证图的连通性
  135. for(int i=;i<=n;i++)//超级源点到每个点的距离为0
  136. {
  137. if(f(s,i))
  138. G[s].push_back(node(i,));
  139. }
  140. int flag=spfa(s,n+);
  141. if(flag==)
  142. printf("lamentable kingdom\n");
  143. else
  144. printf("successful conspiracy\n");
  145. }
  146. return ;
  147. }
  148. /*
  149. 题目意思:
  150. 问你是否存在一个序列S{a1,a2,a3.....an}
  151. 可以满足下面两种不同数量的约束
  152.  
  153. 假设s[x]表示a1+....+ax的和
  154.  
  155. 约束1:x y gt w 比如1 2 gt w
  156. 从a1开始累加,再加2个的和大于w
  157. 根据题目意思即a1+a2+a3>w
  158. 变形一下即s[3]-s[0]>w
  159. 移动位置变形一下:s[0]-s[3]<-w
  160. 继续变形:s[0]-s[3]<=-w-1
  161. 即通式为:s[x-1]-s[x+y]<=-w-1
  162.  
  163. 约束2:x y lt w 比如2 2 lt w
  164. 从a2开始累加,再加两个的和小于w
  165. 即a2+a3+a4<w
  166. 变形一下:s[4]-s[1]<w
  167. 继续变形:s[4]-s[1]<=w-1
  168. 通式:s[x+y]-s[x-1]<=w-1
  169.  
  170. 都是形如x[i]-x[j]<=c的形式
  171. 从j指向i 权值为c这样建图
  172.  
  173. 注意:建图完毕之后存在n+1个点,然后在加一个超级源点s,让s到这n+1个点的距离都为0
  174. 这样是为了保证图的连通性
  175. 然后判断一下图中是否存在负环,存在负环则表示某些约束不能满足
  176. 则不存在这样的序列
  177.  
  178. 加了超级源点之后图中一共有n+2个点!!!
  179.  
  180. 建议spfa判负环
  181. */

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. 用node.js模拟服务器和客户端

    服务器 代码 var net = require("net") var server = net.createServer(); server.listen(12306," ...

  2. 【代码笔记】iOS-自定义选择框(高底强弱)

    一,效果图 二,代码. ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewControl ...

  3. 分享一个 jsPDF的简单使用以及中文编码问题的解决

    后台一个下载文件中内容国际化问题的坑甩到了前端 前端自己匹配,自己处理国际化,生成文件下载 jsPDF-AutoTable    挺靠谱 中文乱码, 还是好人多啊 解决方式如下文章 jsPDF的简单使 ...

  4. VBoxManage翕令

    VBoxManage list vms VBoxManage startvm dcsvr08 -type vrdp VBoxHeadless -startvm "dcsvr08" ...

  5. 《Inside C#》笔记(一) .NET平台

    C# 基于.NET运行时,所以有必要首先对.NET以及C#与.NET平台的关系有一定的了解. 一 .NET平台 .NET背后的基本思想是将原本独立工作的设备.网络服务整合在一个统一的平台上,从而可以为 ...

  6. JAVA 实现 QQ 邮箱发送验证码功能(不局限于框架)

    JAVA 实现 QQ 邮箱发送验证码功能(不局限于框架) 本来想实现 QQ 登录,有域名一直没用过,还得备案,好麻烦,只能过几天再更新啦. 先把实现的发送邮箱验证码更能更新了. 老规矩,更多内容在注释 ...

  7. Hibernate 批处理

    批处理 考虑一种情况,你需要使用 Hibernate 将大量的数据上传到你的数据库中.以下是使用 Hibernate 来达到这个的代码片段: Session session = SessionFact ...

  8. sql server对并发的处理-乐观锁和悲观锁(转)

    假如两个线程同时修改数据库同一条记录,就会导致后一条记录覆盖前一条,从而引发一些问题. 例如: 一个售票系统有一个余票数,客户端每调用一次出票方法,余票数就减一. 情景: 总共300张票,假设两个售票 ...

  9. JavaBeansDataExchange could not instantiate result class

    当ibatis初始化Bean的时候,会调用无参的构造函数,所以如果Bean中有带参的构造函数,一定得多写个无参的构造函数, 否则ibatis会因找不到构造函数而出错,抛出异常如下:JavaBeansD ...

  10. sql行列转换PIVOT与unPIVOT

    基本语法 select * from Mould pivot ( count(ID)for ProductTypeCode in ( [FC], [RCU], [RCD] )) as PVT; wit ...