POJ 1364:King(差分约束)
题目大意:判断是否存在一个长度为n的序列满足给出的不等关系。
分析:
将序列和转化成用两个前缀和之差来表示即可变为差分约束系统。
需要注意的是不能忘记n要加+1,因为还有一个特殊源点,自己因为n:=n+1的位置放在数组预处理的后面所以出错了。
代码:
program king;
type
point=^node;
node=record
x,len:longint; next:point;
end;
var
a:array[..]of point;
q:array[..]of longint;
dis,vis:array[..]of longint;
g:array[..]of boolean;
n,i,m,x,y,v,t,l:longint; c1,c2:char; s1:string; p:point;
procedure put(x,y,v:longint);
var p:point;
begin
new(p);
p^.x:=x; p^.len:=v; p^.next:=a[y];a[y]:=p;
end;
procedure getnew;
var i:longint;
begin
fillchar(q,sizeof(q),);
fillchar(g,sizeof(g),false); fillchar(vis,sizeof(vis),);
for i:= to n do dis[i]:=maxlongint div ;
for i:= to n do begin dispose(a[i]); new(a[i]); a[i]:=nil; end;
dis[n]:=; g[n]:=true; q[]:=n;
end;
function spfa:boolean;
var x,y,h,t:longint; p:point;
begin
h:=; t:=;
while h<t do
begin
inc(h); x:=q[h]; g[x]:=false; new(p);p:=a[x];
while p<>nil do
begin
y:=p^.x;
if dis[x]+p^.len<dis[y] then
begin
dis[y]:=dis[x]+p^.len;
if g[y]=false then
begin
inc(t); q[t]:=y; g[y]:=true;
end;
inc(vis[y]); if vis[y]>n then exit(false);
end;
p:=p^.next;
end;
end;
exit(true);
end;
begin
read(n);
while n> do
begin
inc(n); getnew;
read(m); readln;
for i:= to n- do put(i,n,);
for i:= to m do
begin
readln(s1);
t:=pos('t',s1); l:=pos(' ',s1);
val(copy(s1,,l-),x); val(copy(s1,l+,t--l),y);val(copy(s1,t+,length(s1)-t-),v);
if s1[t-]='l' then put(x+y,x-,v-);
if s1[t-]='g' then put(x-,x+y,-v-);
end;
if spfa then writeln('lamentable kingdom') else writeln('successful conspiracy');
read(n);
end;
end.
POJ 1364:King(差分约束)的更多相关文章
- POJ 1364 King --差分约束第一题
题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析 ...
- [poj 1364]King[差分约束详解(续篇)][超级源点][SPFA][Bellman-Ford]
题意 有n个数的序列, 下标为[1.. N ], 限制条件为: 下标从 si 到 si+ni 的项求和 < 或 > ki. 一共有m个限制条件. 问是否存在满足条件的序列. 思路 转化为差 ...
- poj 1364 King(差分约束)
题目:http://poj.org/problem?id=1364 #include <iostream> #include <cstdio> #include <cst ...
- POJ 1364 King (UVA 515) 差分约束
http://poj.org/problem?id=1364 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemi ...
- poj 1364 King(差分约束)
题意(真坑):傻国王只会求和,以及比较大小.阴谋家们想推翻他,于是想坑他,上交了一串长度为n的序列a[1],a[2]...a[n],国王作出m条形如(a[si]+a[si+1]+...+a[si+ni ...
- POJ 1364 King (差分约束)
King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8660 Accepted: 3263 Description ...
- POJ 1364 King
http://poj.org/problem?id=1364 题意 :给出一个序列a1,a2,a3,a4.....ai,......at ;然后给你一个不等式使得ai+a(i+1)+a(i+2)+.. ...
- poj 1201 Intervals(差分约束)
题目:http://poj.org/problem?id=1201 题意:给定n组数据,每组有ai,bi,ci,要求在区间[ai,bi]内至少找ci个数, 并使得找的数字组成的数组Z的长度最小. #i ...
- poj 1201 Intervals——差分约束裸题
题目:http://poj.org/problem?id=1201 差分约束裸套路:前缀和 本题可以不把源点向每个点连一条0的边,可以直接把0点作为源点.这样会快许多! 可能是因为 i-1 向 i 都 ...
- POJ——1364King(差分约束SPFA判负环+前向星)
King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11946 Accepted: 4365 Description ...
随机推荐
- XGBoost算法原理小结
在两年半之前作过梯度提升树(GBDT)原理小结,但是对GBDT的算法库XGBoost没有单独拿出来分析.虽然XGBoost是GBDT的一种高效实现,但是里面也加入了很多独有的思路和方法,值得单独讲一讲 ...
- 委托、事件与Observer设计模式
范例说明 上面的例子已不足以再进行下面的讲解了,我们来看一个新的范例,因为之前已经介绍了很多的内容,所以本节的进度会稍微快一些: 假设我们有个高档的热水器,我们给它通上电,当水温超过95度的时候:1. ...
- oc字典放入到数组里,根据字典里的属性排序(重点)
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { ...
- build path导入的jar失效导致找不到类
今天碰到一个很奇葩的问题,搞起我以后都不敢 build path到jar了 所以我就全部放到lib目录下了,因为之前使用build path导入的jar失效了,一直找不类,具体原因我也不清楚,我之前的 ...
- vue.js devtools安装
在调试的时候chrome会提示安装vue devtools,我以为是要在chrome的程序商店直接安装,但是国内对谷歌的和谐,无法访问谷歌商店内容 官方有源代码,可以下载下来自行编译安装 官方地址:h ...
- PHP 根据IP获取地理位置
/** * 根据用户IP获取用户地理位置 * $ip 用户ip */ function get_position($ip){ if(empty($ip)){ return '缺少用户ip'; } $u ...
- 开源数据库中间件-MyCat
开源数据库中间件-MyCat产生的背景 如今随着互联网的发展,数据的量级也是成指数的增长,从GB到TB到PB.对数据的各种操作也是愈加的困难,传统的关系型数据库已经无法满足快速查询与插入数据的需求.这 ...
- ubuntu下vim的简单配置
该文章只是进行符合自己习惯的最基本的配置,更加高级的配置请参考更加有含量的博文! 1.打开vim下的配置文件 sudo vim /etc/vim/vimrc 2.在这个文件中,会有这么一句:synta ...
- GoF23种设计模式之创建型模式之工厂方法模式
一.概述 定义一个用于创建对象的接口,让子类去决定实例化哪个类.工厂方法将一个类的实例化延迟至其子类. 二.适用性 1.当一个类不知道它所必须创建的对象的类的时候. 2.当一个类希望由其子类来指定它所 ...
- Farm Tour POJ - 2135 (最小费用流)
When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= ...