POJ1364 King
Description
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
Output
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
Source
正解:SPFA+差分约束系统
解题报告:
题目大意是给定一段区间的和小于或者大于某个值,然后问是否存在这种序列。
考虑用点做差分约束的话感觉无从下手,于是我们可以想到用前缀和的形式,首末来加边。比如Sy-Sx-1<=z 则添加一条x-1到y的权值为z的边
然后这道题比较水,我们只需要判断是否存在负权环就可以了。值得一提的是我们需要一开始就把所有结点加进队列,并且把所有的dis置为0就可以了。因为只要存在负权环就一定会不断入队,判断一下次数大于某个值就可以break了
//It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#ifdef WIN32
#define OT "%I64d"
#else
#define OT "%lld"
#endif
using namespace std;
typedef long long LL;
const int inf = (<<);
const int MAXN = ;
const int MAXM = ;
int n,m;
int dis[MAXN];
int first[MAXN],to[MAXM],next[MAXM],w[MAXM];
int ecnt;
queue<int>Q;
char ch[];
bool pd[MAXN];
int cnt[MAXN]; inline int getint()
{
int w=,q=;
char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar();
if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar();
return q ? -w : w;
}
inline void Init(){
memset(first,,sizeof(first));
ecnt=;
memset(pd,,sizeof(pd));
while(!Q.empty()) Q.pop();
memset(cnt,,sizeof(cnt));
} inline bool spfa(){
for(int i=;i<=n;i++) Q.push(i),pd[i]=;
for(int i=;i<=n;i++) dis[i]=;
while(!Q.empty()){
int u=Q.front(); Q.pop(); pd[u]=;
for(int i=first[u];i;i=next[i]){
int v=to[i];
if(dis[v]>dis[u]+w[i]) {
dis[v]=dis[u]+w[i];
if(!pd[v]) {
cnt[v]++;
if(cnt[v]>=n) return false;
Q.push(v); pd[v]=;
}
}
}
}
return true;
} inline void solve(){
while(true){
n=getint();
if(n==) break;
m=getint();
Init();
int x,y,z;
for(int i=;i<=m;i++) {
x=getint();y=getint(); scanf("%s",ch); z=getint();
if(ch[]!='g') { next[++ecnt]=first[x-];to[ecnt]=x+y;first[x-]=ecnt;w[ecnt]=z-; }
else{ next[++ecnt]=first[x+y];to[ecnt]=x-;first[x+y]=ecnt;w[ecnt]=-z-; }
}
if(!spfa()) printf("successful conspiracy\n");
else printf("lamentable kingdom\n");
}
} int main()
{
solve();
return ;
}
POJ1364 King的更多相关文章
- 转自作者:phylips@bmy
差分约束系统 2008-11-28 20:53:25| 分类: 算法与acm|举报|字号 订阅 出处:http://duanple.blog.163.com/blog/static/7097 ...
- poj图论解题报告索引
最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman- ...
- ZOJ1260/POJ1364国王(King)
// 题意 问是否存在一个长度为n的序列// 这个序列满足m个限制// 每个限制有 si ni oi kisi 为序列位置 ni为从si开始连续长度为ni+1 的子序列 这些子序列和 大于或小于 ki ...
- BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3336 Solved: 1936[Submit][ ...
- [bzoj1087][scoi2005]互不侵犯king
题目大意 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上 左下右上右下八个方向上附近的各一个格子,共8个格子. 思路 首先,搜索可以放弃,因为这是一 ...
- King's Quest —— POJ1904(ZOJ2470)Tarjan缩点
King's Quest Time Limit: 15000MS Memory Limit: 65536K Case Time Limit: 2000MS Description Once upon ...
- 【状压DP】bzoj1087 互不侵犯king
一.题目 Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上.下.左.右,以及左上.左下.右上.右下八个方向上附近的各一个格子,共8个格子. I ...
- ZOJ 2334 Monkey King
并查集+左偏树.....合并的时候用左偏树,合并结束后吧父结点全部定成树的根节点,保证任意两个猴子都可以通过Find找到最厉害的猴子 Monkey King ...
- ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 K. King’s Rout
K. King's Rout time limit per test 4 seconds memory limit per test 512 megabytes input standard inpu ...
随机推荐
- 手工创建tomcat应用,以及实现js读取本地文件内容
手工创建tomcat应用: 1.在webapps下面新建应用目录文件夹 2.在文件夹下创建或是从其他应用中复制:META-INF,WEB-INF这两个文件夹, 其中META-INF清空里面,WEB-I ...
- Cookie测试工具小汇
现在很多网站都用到Cookies,特别是用户的登陆以及购物网站的购物车. Cookies 通常用来存储用户信息和用户在某应用系统的操作,当一个用户使用Cookies 访问了某一个应用系统时,Web 服 ...
- java7-3 继承
1.继承概述: 把多个类中相同的内容给提取出来定义到一个类中. 如何实现继承呢? Java提供了关键字:extends 格式: class 子类名 extends 父类名 {} 父类也称为基类.超类: ...
- smarty初始化文件
为了smarty初始化文件能方便其他目录下的脚步包含使用,应该将初始化文件里的引入smarty类及其他路径设置为全局路径,而不是相对路径 另外,如果模板里有文件的路径的引用,一定要是相对PHP脚本文件 ...
- Chrome 开发工具 Javascript 调试技巧
http://www.w3cplus.com/tools/dev-tips.html 一.Sources 面板介绍: Sources 面板分为左中右 3 部分左:Sources 当前页面加载的资源列表 ...
- 一。常用UIView的属性和方法
1.frame 控件所在的矩形框的位置和尺寸(以父控件的左上角为坐标原点) 2.bounds 控件 控件所在的矩形框的位置和尺寸(以自己的左上角为坐标原点,所以bounds的x/y一般为0) 3.ce ...
- pc加入域认证细节
计算机组织形式 工作组(无法统一管理,无法统一身份验证) 域 win权限分配机制 管理本地用户 新建用户-加入管理员组. 针对文件夹基于(用户组)设置权限 用户SID 创建一个用户时候,每个用户都有一 ...
- 各种同步方法性能比较(synchronized,ReentrantLock,Atomic)
5.0的多线程任务包对于同步的性能方面有了很大的改进,在原有synchronized关键字的基础上,又增加了ReentrantLock,以及各种Atomic类.了解其性能的优劣程度,有助与我们在特定的 ...
- 对EBS中配置文件的初步认识
配置文件(PROFILE)在EBS系统配置占有很重要的位置,功能顾问要对很多重要的配置文件做到非常熟悉才行.否则出现一个问题,可能在郁闷许久后,发觉只是某个不起眼的配置文件在捣乱.配置文件相当于带有权 ...
- HTTP基础(一):如何使用浏览器network查看请求和响应的信息
一. 问题描述 HTTP作为前端开发与后开发链接的载体,其重要性不言而喻,今天我不复习关于HTTP自身的一些知识,只复习如何解读浏览器自带的的抓包工具(查看请求信息与响应信息)network. 二. ...