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.
 
对于每个约束条件都可以列出一个不等式 我们把每个不等式转化成ai-bi<=ki的形式 构图 添加一条bi到ai的权值为ki的边 然后判负圈即可
 
 #include<iostream>
 #include<cstdio>
 #include<cstring>
 #include<algorithm>
 #include<cmath>
 #include<vector>
 #include<queue>
 using namespace std;
 ;
 <<;
 struct Edge{
     int from,to,dist;
 };
 vector<Edge> edges;
 vector<int> G[maxn];
 int n,m,x,y,k,d[maxn],inq[maxn],cnt[maxn];
 ];
 void init(){
     edges.clear();
     ;i<=n+;i++) G[i].clear();
 }
 bool BF(int s){
     memset(inq,,sizeof(inq));
     memset(cnt,,sizeof(cnt));
     ;i<=n+;i++) d[i]=inf;
     d[s]=;inq[s]=;
     queue<int> Q;
     Q.push(s);
     while(!Q.empty()){
         int u=Q.front();Q.pop();
         inq[u]=;
         ;i<G[u].size();i++){
             Edge &e=edges[G[u][i]];
             int go=e.to;
             if(d[go]>d[u]+e.dist){
                 d[go]=d[u]+e.dist;
                 if(!inq[go]){
                     Q.push(go);
                     inq[go]=;
                     if(++cnt[go]>n) return false;
                 }
             }
         }
     }
     return true;
 }
 void addEdge(int from,int to,int dist){
     Edge e;
     e.dist=dist,e.from=from,e.to=to;
     edges.push_back(e);
     int m=edges.size();
     G[);
 }
 ;
 int main()
 {
     while(scanf("%d",&n)&&n){
         scanf("%d",&m);
         init();
         ;i<=m;i++){
             scanf("%d%d%s%d",&x,&y,op,&k);
             ){
                 addEdge(x+y,x-,--k);
             }else{
                 addEdge(x-,x+y,k-);
             }
         }
         ;i<=n;i++){
             addEdge(n+,i,);
         }
         ;
         else printf("\n");
         //不同的OJ可能评判方式不同 要注意输出最后是否有回车
         )){
             printf("successful conspiracy\n");
         }else{
             printf("lamentable kingdom\n");
         }
     }
     ;
 }

POJ1364 King-差分的更多相关文章

  1. POJ1364 King

    Description Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen p ...

  2. POJ 1364 King --差分约束第一题

    题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析 ...

  3. [poj 1364]King[差分约束详解(续篇)][超级源点][SPFA][Bellman-Ford]

    题意 有n个数的序列, 下标为[1.. N ], 限制条件为: 下标从 si 到 si+ni 的项求和 < 或 > ki. 一共有m个限制条件. 问是否存在满足条件的序列. 思路 转化为差 ...

  4. poj1364(差分约束系统)

    poj1364 设s[i] 表示a1 + a2 + ... + a(i-1)的和 给我们n个点,m条约束 如果是a b gt c    那么表示 s[a+b+1] - s[a] > c     ...

  5. King 差分约束 判负环

    给出n个不等式 给出四个参数第一个数i可以代表序列的第几项,然后给出n,这样前面两个数就可以描述为ai+a(i+1)+...a(i+n),即从i到n的连续和,再给出一个符号和一个ki当符号为gt代表‘ ...

  6. UVALive 5532 King(差分约束,spfa)

    题意:假设一个序列S有n个元素,现在有一堆约束,限制在某些连续子序列之和上,分别有符号>和<.问序列S是否存在?(看题意都看了半小时了!) 注意所给的形式是(a,b,c,d),表示:区间之 ...

  7. poj 1364 King(差分约束)

    题目:http://poj.org/problem?id=1364 #include <iostream> #include <cstdio> #include <cst ...

  8. King(差分约束)

    http://poj.org/problem?id=1364 题意:输入i,n,gt(lt),k; 判断是否存在这样一个序列,从第 i 项加到第 n+i 项的和 <(lt) k 或 >(g ...

  9. poj图论解题报告索引

    最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman- ...

  10. 【转】最短路&差分约束题集

    转自:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★254 ...

随机推荐

  1. 怎么做一个bat文件

    怎么做一个bat文件 | 浏览:639 | 更新:2014-11-25 17:02 1 2 3 4 5 6 7 分步阅读 其实做一个.BAT文件很简单,下面我就以做一个清除系统垃圾.BAT文件为例,给 ...

  2. 初始化脚本(Os_Init_Optimization.sh)

    #!/bin/bash #解压缩startup.tar.gz包 cd /tmp && tar -zxf startup.tar.gz #初始化YUM源 rm -rf /etc/yum. ...

  3. java基础介绍(转)

    很全面的介绍JAVA基础的文档 Java 编程简介,第 1 部分: Java 语言基础 http://www.ibm.com/developerworks/cn/java/j-introtojava1 ...

  4. 关于QGraphicsScene 和 QGraphicsView 和 QDialog 的杂乱笔记【或说指针复习。。】

    LtCalibrateDlg::~LtCalibrateDlg() { if (m_pIplImageGray) cvReleaseImage(&m_pIplImageGray); MYDEL ...

  5. Python time模块学习

    Python time模块提供了一些用于管理时间和日期的C库函数,由于它绑定到底层C实现,因此一些细节会基于具体的平台. 一.壁挂钟时间 1.time() time模块的核心函数time(),它返回纪 ...

  6. BackTrack5-r3安装前需要的准备及说明

    一. 配置创建一个虚拟机,本教程用的是VMware-workstation-full-10.0.0,BT5-r3-GNOME-64镜像. 这里是BT5-r3-GNOME-64位种子:http://pa ...

  7. Win10/UWP新特性系列-GetPublisherCacheFolder

    微软Windows Runtime App拥有很强的安全模型来防止不同App之间的数据获取和共享,也就是我们所说的"沙盒机制",每个App都运行在Windows沙盒中,App之间的 ...

  8. Word或者Excel中怎么把 "空格" 替换成 "换行 "

    word中ctrl+h打开替换,将" "替换为^pexcel替换成alt+小键盘区的10

  9. 华为HG255D路由器使用OH3C进行中大校园网认证

    之前用的上海贝尔RG100A-AA路由器,被我无情地摧残了,电源按钮挂了,只能换个路由器.由于在校内,使用OP还是比较方便的,网上淘了这款华为HG255D,店主已刷好OP,无线速率300M,想想也是值 ...

  10. linux dns 连外网

    以下设置对所用的Linux系统如Redhat/Ubuntu/Debian/CentOS等都有效,但您必须是管理员root或者具有管理员权限 vim /etc/resolv.conf 在其中加入: na ...