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. 【性能测试工具ab】ab工具使用

    1.在安装了apache服务器后,或者wampserver后,在bin目录下,有一个ab.exe文件 2.使用,进入ab.exe所在的文件夹 3.ab -c   10 -n  1000     htt ...

  2. 逆袭之旅DAY24.XIA.数组练习

    2018-07-20 08:40:19 1. public void stringSort(){ String[] s = new String[]{"George"," ...

  3. maven scope和项目发布需要注意的地方

    Maven Scope的使用: http://www.cnblogs.com/wangyonghao/p/5976055.html servlet-api和jsp-api等jar包,一般由servle ...

  4. SpringWeb项目常用注解简单介绍

    注解分为两类: 一类是使用Bean,即是把已经在xml文件中配置好的Bean拿来用,完成属性.方法的组装:比如@Autowired , @Resource,可以通过byTYPE(@Autowired) ...

  5. 5.3 C++用顶层函数重载操作符

    参考:http://www.weixueyuan.net/view/6381.html 总结: 可以将操作符重载函数声明为顶层函数. 如果以顶层函数的形式重载操作符时,二元操作符重载函数必须有两个参数 ...

  6. 2.16 C++类与new和delete操作符

    参考: http://www.weixueyuan.net/view/6347.html 总结: 当我们需要为类对象动态分配存储空间时,我们应该使用C++语言提供的new与new[]操作符,而不要使用 ...

  7. 基于MicroBlaze 的嵌入式系统设计

       reference: http://xilinx.eetrend.com/d6-xilinx/article/2013-03/3863.html 摘 要:当今时代,嵌入式系统已经无所不在,与人们 ...

  8. 关于js中函数的调用问题

    js中函数的调用方法 1.直接调用 函数名(参数): 2.通过指向函数的变量去调用 例如: var myval = 函数名: 此刻 myval是指向函数的一个指针: myval(实际参数):此刻调用的 ...

  9. mvc Model验证总结及常用正则表达式

    本文属转载,来源: http://www.byywee.com/page/M0/S868/868615.html 关于Model验证官方资料: http://msdn.microsoft.com/zh ...

  10. day 42 mysql 数据类型

    mysql 数据类型   数据类型 truncate t1 (删除表) # 无符号类型 alter table t1 modify id tinyint unsigned (表里有值,不能直接改) d ...