http://acm.hdu.edu.cn/showproblem.php?pid=1531

King

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1992    Accepted Submission(s): 928

Problem 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
题目大意:给了一些不等式,让验证是否存在一种情况使之成立。
题目分析:典型的差分约束题,用SPFA判断构建好的图是否存在正环【如果跑最短路则看是否存在负环】。而不同于之前的差分约束,这些不等式构建出的图不一定连通,这时需要加一个超级源点【n+1】,并且该点到其余点【0~n】都要有一条权值为0的边,引入超级源点之后即和普通差分约束一样了,跑一下SPFA即可
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
struct edge{
int to;
int next;
int len;
}qwq[];
queue<int>qaq;
int edge_cnt,n,m,in[],stk[],dist[],head[];
void add(int x,int y,int z)
{
qwq[edge_cnt].len = z;
qwq[edge_cnt].to = y;
qwq[edge_cnt].next=head[x];
head[x]=edge_cnt++;
}
bool spfa()
{
while(!qaq.empty())
{
qaq.pop();
}
dist[n+]=;
qaq.push(n+);
stk[n+]=;
in[n+]++;
while(!qaq.empty())
{
int orz=qaq.front();qaq.pop();
stk[orz]=;
for(int i = head[orz] ; i != - ; i=qwq[i].next)
{
int v=qwq[i].to;
if(dist[v]<dist[orz]+qwq[i].len)
{
dist[v]=dist[orz]+qwq[i].len;
if(!stk[v])
{
in[v]++;
qaq.push(v);
stk[v]=;
if(in[v]>n+)
return false;
}
}
}
}
return true;
}
int main()
{
// int n,m;
scanf("%d",&n);
while(n)
{
edge_cnt = ;
memset(dist,-,sizeof(dist));
memset(head,-,sizeof(head));
memset(in,,sizeof(in));
memset(stk,,sizeof(stk));
edge_cnt=;
scanf("%d",&m);
while(m--)
{
int a,b,c;
char d,e;
scanf("%d %d %c%c %d",&a,&b,&d,&e,&c);
if(d=='g')
{
add(a-,a+b,c+);
}
else
{
add(a+b,a-,-c+);
} }
for(int i = ; i <= n ; i++)
{
add(n+,i,);
}
if(spfa())printf("lamentable kingdom\n");
else printf("successful conspiracy\n");
scanf("%d",&n);
}
return ;
}

【HDOJ1531】【差分约束+添加超级源点】的更多相关文章

  1. HDU 2680 最短路 迪杰斯特拉算法 添加超级源点

    Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  2. POJ 1364 King (差分约束)

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

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

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

  4. poj 1364 King(线性差分约束+超级源点+spfa判负环)

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

  5. 图论--差分约束--POJ 3169 Layout(超级源汇建图)

    Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 < ...

  6. spfa+差分约束系统(D - POJ - 1201 && E - POJ - 1364&&G - POJ - 1)+建边的注意事项+超级源点的建立

    题目链接:https://cn.vjudge.net/contest/276233#problem/D 具体大意: 给出n个闭合的整数区间[ai,bi]和n个整数c1,-,cn. 编写一个程序: 从标 ...

  7. Is the Information Reliable?(差分约束)

    Description The galaxy war between the Empire Draco and the Commonwealth of Zibu broke out 3 years a ...

  8. 鉴于spfa基础上的差分约束算法

    怎么搞?        1. 如果要求最大值      想办法把每个不等式变为标准x-y<=k的形式,然后建立一条从y到x权值为k的边,变得时候注意x-y<k =>x-y<=k ...

  9. hdu 1531(差分约束)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1531 差分约束的题之前也碰到过,刚好最近正在进行图论专题的训练,就拿来做一做. ①:对于差分不等式,a ...

随机推荐

  1. 【Eclipse使用】在eclipse里添加源文件和Api的方法

    一.源代码添加 你的JDK安装目录下%Java_home%/src.zip文件就是源码,解压缩找到对应包下面的类即可. 如果是Eclipse开发,ctr+鼠标左击,出现不了源码的话,在弹出的视图中点击 ...

  2. Win10系列:JavaScript图形

    在页面中添加canvas元素会在页面上生成一个矩形的位图画布,可以使用JavaScript在画布上实时绘制图形图像.在绘制图形时,需要先调用画布的getContext函数获取与该画布相关的用于绘制图形 ...

  3. API服务网关(Zuul)

    技术背景 前面我们通过Ribbon或Feign实现了微服务之间的调用和负载均衡,那我们的各种微服务又要如何提供给外部应用调用呢. 当然,因为是REST API接口,外部客户端直接调用各个微服务是没有问 ...

  4. Python之路-python基础二(补充)

    本章内容: 三元运算 八进制,十六进制,十进制与二进制的转换 集合的修改方法 字符串常用方法            三元运算  三元运算简化了if else的语句,将四行代码简化为一行.三元运算的格式 ...

  5. hibernate创建构架

    创建hibernate架构: 注意:需要将所需的架包导进去: 二:Java工程的具体结构: 具体代码如下:hibernate.cfg.xml <!DOCTYPE hibernate-config ...

  6. java获得当前系统时间三种方法

    参见: http://blog.csdn.net/cloume/article/details/46624637

  7. Springboot+MyBatis+mysql+jsp页面跳转详细示例

           SpringBoot与MyBatis搭建环境,底层数据库为mysql,页面使用JSP(官网上不推荐使用jsp),完成从数据库中查询出数据,在jsp页面中显示,并且实现页面的跳转功能. 项 ...

  8. Oauth2.0:Access Token 与 Refresh Token

    access token 是客户端访问资源服务器的令牌.拥有这个令牌代表着得到用户的授权.然而,这个授权应该是临时的,有一定有效期.这是因为,access token 在使用的过程中可能会泄露.给 a ...

  9. SQL-11 获取所有员工当前的manager,如果当前的manager是自己的话结果不显示

    题目描述 获取所有员工当前的manager,如果当前的manager是自己的话结果不显示,当前表示to_date='9999-01-01'.结果第一列给出当前员工的emp_no,第二列给出其manag ...

  10. UUID+随机数

    import java.util.Random; import java.util.UUID; public class UUIDUtils { public static String getUUI ...