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. vs2017 Mariadb/mysql之旅

    记录vs2017使用 ef6+mysql的开发 填坑之旅.我的环境 vm+centos7+ docker-ce+mariadb+vs2017 总的原则是MySql.Data.Entity 要和 mys ...

  2. Hive/hbase/sqoop的基本使用教程~

    Hive/hbase/sqoop的基本使用教程~ ###Hbase基本命令start-hbase.sh     #启动hbasehbase shell      #进入hbase编辑命令 list  ...

  3. selenium(六)Headless Chrome/Firefox--PhantomJS停止支持后,使用无界面模式。

    简介: 以前都用PhantomJS来进行无界面模式的自动化测试,或者爬取某些动态页面. 但是最近selenium更新以后,'Selenium support for PhantomJS has bee ...

  4. Toy Factory

    Factory is a design pattern in common usage. Please implement a ToyFactory which can generate proper ...

  5. Spring框架的四大原则

    Spring框架本身有四大原则: 1).使用POJO进行轻量级和最小入侵式开发 2).通过以来注入和基于接口编程实现松耦合 3).通过AOP和默认习惯进行声明式编程 4).使用AOP和模板减少模式化代 ...

  6. Final阶段第1周/共1周 Scrum立会报告+燃尽图 06

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2485] 版本控制:https://git.coding.net/liuyy08 ...

  7. oracle sql developer登录

    1 登录Oracle SQL developer 时候要选择数据库连接,这里要区分cdb用户和pdb用户,cdb用户可以在cdb和pdb服务下登录,而pdb用户只能在pdb服务里面登录.比如sys用户 ...

  8. ESP8266上报数据到中国移动物联网平台HTTP

    #include <HttpPacket.h> #include <ArduinoJson.h> #include <ESP8266WiFi.h> HttpPack ...

  9. python day09--定义函数

    一.函数的定义 def  函数名(参数): 函数体 来我们来定义⼀一个约x功能: def yue(): print("拿出⼿手机") print("打开陌陌") ...

  10. python day03作业