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 ...
随机推荐
- AutoIT 实现Firefox上传文件
Firefox浏览器文件上传代码如下: ;upload file Func _UploadFile($file) AutoItSetOption("WinTitleMatchMode&quo ...
- STL之stack栈
栈(statck)这种数据结构在计算机中是相当出名的.栈中的数据是先进后出的(First In Last Out, FILO).栈只有一个出口,允许新增元素(只能在栈顶上增加).移出元素(只能移出栈顶 ...
- 类图和对象图教程-类(Class)、接口(Interface)、协作(collaboration)、依赖关系(Dependency)、泛化关系(Generalization)、关联关系(Association)以及实现关系(Realization)
类图的概念 (转) 一.概述 类图(Class Diagram)是描述类.接口.协作以及它们之间关系的图,用来显示系统中各个类的静态结构.类图是定义其他图的基础,在类图基础上,可以使用状态图.协作图. ...
- 【转】【C#】判断两个文件是否相同
使用System.security.Cryptography.HashAlgorithm类为每个文件生成一个哈希码,然后比较两个哈希码是否相同 该哈希算法为一个文件生成一个小的二进制“指纹”,从统计学 ...
- 【转】【C#】C#重绘windows窗体标题栏和边框
摘要 windows桌面应用程序都有标准的标题栏和边框,大部分程序也默认使用这些样式,一些对视觉效果要求较高的程序,如QQ, MSN,迅雷等聊天工具的样式则与传统的windows程序大不相同,其中迅雷 ...
- 结合C++和GDAL实现shapefile(shp)文件的创建和写入
工具:vs2012+GDAL 2.0 包含头文件: #include "ogrsf_frmts.h" int main() { const char *pszDriverName ...
- 虚拟机安装Mac OS X ----- VM12安装Mac OS X
Windows下虚拟机安装Mac OS X -– VM12安装Mac OS X 10.11 随着Iphone在国内大行其道,越来越多的开发者涌入iOS开发大军 中,但都苦于没有苹果机,本文即将介绍WI ...
- AWS S3使用小结
使用场景一:储存网站的图片,并能被任何人访问 1. 创建一个bucket,名字与需要绑定的域名一致. 例如,根域名是mysite.com,希望把所有图片放在pic.mysite.com下面,访问的时候 ...
- Openwrt flash 空间不足的临时解决方法
最近有网友在安装软件的时候发现flash空间不够用了: 一个临时的解决方案是在RAM里面使用这个程序.因为 1.路由器改机后的RAM有64MB,flash一般有16MB,RAM空间比较大./tmp是挂 ...
- C#基础之IL
1.实例解析IL 作为C#程序员,IL的作用不言而喻,首先来看一个非常简单的程序和它的IL解释图,通过这个程序的IL指令来简单的了解常见的IL指令是什么意思. class Program { stat ...