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. Entrust - Laravel 用户权限系统解决方案

    Zizaco/Entrust 是 Laravel 下 用户权限系统 的解决方案, 配合 用户身份认证 扩展包 Zizaco/confide 使用, 可以快速搭建出一套具备高扩展性的用户系统. Conf ...

  2. learning ddr mode reigsters

    For application flexibility, various functions, features, and modes are programmable in four Mode Re ...

  3. Ant在Java项目中的使用(一眼就看会)

    参考:http://www.cnblogs.com/zhengqiang/p/5557155.html Ant是跨平台的构建工具,它可以实现项目的自动构建和部署等功能.在本文中,主要让读者熟悉怎样将A ...

  4. 每天CSS学习之direction

    direction是CSS2的属性,它的作用是规定文字书写的方向. 1.ltr:从左到右,即left to right.该值为默认值.如下所示: div{ border:1px solid red; ...

  5. Linux如何产看系统信息

    如何查看已安装的CentOS版本信息: 1)[root@localhost ~]# cat /proc/version Linux version 2.6.18-194.el5 (mockbuild@ ...

  6. Calendar获取当前年份、月份、日期

    import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Te ...

  7. Android开发---如何操作资源目录中的资源文件3--圆角边框、背景颜色渐变效果、边框颜色

    Android开发---如何操作资源目录中的资源文件3 效果图 1.圆角边框 2.背景颜色渐变效果 1.activity_main.xml 描述: 定义了一个shape资源管理按钮 <?xml ...

  8. DOS debug 命令的详细用法

    DOS下的DEBUG命令的详细用法       2 推荐 名称 解释 格式 a (Assemble) 逐行汇编 a [address] c (Compare) 比较两内存块 c range addre ...

  9. VC下CString类型与int 、float等数据类型的相互转换

    一.常用转换 1. CString --> int转换 CString str("1234");    int i= _ttoi(str); 2. CString --> ...

  10. python 爬虫数据准换时间格式

    timeStamp = 1381419600 dateArray = datetime.datetime.utcfromtimestamp(timeStamp) otherStyleTime = da ...