POJ 2983:Is the Information Reliable?(差分约束)
题目大意:有n个点在一条直线上,有两类关系:P(x,y,v)表示x在y北边v距离处,V(x,y)表示x在y北边至少1距离出,给出一些这样的关系,判断是否有矛盾。
分析:
差分约束模板题,约束条件P:a-b>=v a-b<=v即a-b>=v b-a<=-v,V:a-b>=1即b-a<=-1,构图spfa判负权回路即可。
需要注意 的是存图时不能用一个二维数组存边权,因为两点间会有多条边。、
spfa判断负权回路的方法,记录每个点进队次数,超过点的数目即存在负权回路。
代码:
program infor;
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:longint; c:char; 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[]:=; g[]:=true; q[]:=;
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
readln(n,m);
while (n<>)or(m<>) do
begin
getnew;
for i:= to n do put(i,,);
for i:= to m do
begin
read(c);
if c='P' then
begin read(x,y,v); put(x,y,v); put(y,x,-v); end
else if c='V' then begin read(x,y); put(y,x,-); end;
if i<m then readln;
end;
if spfa then writeln('Reliable') else writeln('Unreliable');
n:=; m:=;
readln(n,m);
end;
end.
POJ 2983:Is the Information Reliable?(差分约束)的更多相关文章
- POJ 2983 Is the Information Reliable? 差分约束
裸差分约束. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...
- POJ 2983 Is the Information Reliable? 依旧差分约束
http://poj.org/problem?id=2983 题目大意: 星际大战开始了.你购买了情报,需要判断它的准确性.已知地方的根据地在由南向北排成一条直线.P A B X,表示A在B北面距离X ...
- POJ 2983 Is the Information Reliable? 信息可靠吗 (差分约束,spfa)
题意:有n个站排成一列,针对每个站的位置与距离关系,现有多个约束条件,约束条件分两种:(1)确定的.明确说明站a距离站b多少个单位距离.(2)不确定的.只知道a在b的左边至少1个单位距离. 根据已知 ...
- POJ 2983 Is the Information Reliable?(差分约束系统)
http://poj.org/problem?id=2983 题意:N 个 defense stations,M条消息,消息有精确和模糊之分,若前边为P.则为精确消息,有两个defense stati ...
- ●POJ 2983 Is the Information Reliable?
题链: http://poj.org/problem?id=2983 题解: 差分约束. 1).对于条件(P u v w),不难发现反映到图上就是: $dis[u]-dis[v]=w$,所以添加两条边 ...
- POJ 2983-Is the Information Reliable?(差分约束系统)
题目地址:POJ 2983 题意:有N个车站.给出一些点的精确信息和模糊信息.精确信息给出两点的位置和距离.模糊信息给出两点的位置.但距离大于等于一.试确定是否全部的信息满足条件. 思路:事实上就是让 ...
- POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)
原题地址:http://poj.org/problem?id=3159 题意大概是班长发糖果,班里面有不良风气,A希望B的糖果不比自己多C个.班长要满足小朋友的需求,而且要让自己的糖果比snoopy的 ...
- POJ 之 Is the Information Reliable?
B - Is the Information Reliable? Time Limit:3000MS Memory Limit:131072KB 64bit IO Format:%I6 ...
- Is the Information Reliable?(差分约束系统)
http://poj.org/problem?id=2983 题意:给出M条信息,判断这些信息的正确性.(1)V A B :表示A,B之间的距离>=1; (2)P A B X :表示A B之间的 ...
- POJ 3159 Candies(spfa、差分约束)
Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the he ...
随机推荐
- 集成Ehcache
提醒 这一小节的是如何在应用层(service或者module或action类)中使用ehcache 准备工作 下载ehcache 你需要一个js文件 请务必阅读下面代码中的注释!! 分情况选 ...
- Matlab将多幅图片保存为mat
%% 储存某目录所有的图片 pt = 'd:\imgs\'; ext = '*.jpg'; dis = dir([pt ext]); nms = {dis.name}; for k = 1:lengt ...
- ScriptMaker
0x00 前言 pwn脚本千篇一律,之前也是保存了一份模板,每次都用它,但还是觉得每次都复制一次各种名字还是有的累,于是就写了一份脚本生成器 0x01 ScriptMaker #!/usr/bin/e ...
- 安装mysql提示This application requires .NET framework 4.0.
问题描述:安装MySQL社区版时遇到This application requires .NET framework 4.0. 解决方法:在http://search.microsoft.com/zh ...
- JavaScript中的match方法和search方法
search在一个字串对象(string object)中查找关键词字串(规范表达式,regular expression),若匹配(即在目标字串中成功找到关键词)则返回关键词在目标字串中第一次出现的 ...
- C/C++程序基础 (七)继承和多态
多态 编译时多态:重载 运行时多态:虚函数.根据运行时的类别获取正确的虚指针,从而定位正确的虚函数. 虚函数 虚函数指针:指向虚函数表.多重继承则含有多个基类的虚函数指针. 虚函数表:函数指针集合.普 ...
- Mysql 查询出某列字段 被包含于 条件数据中
我们通常是使用 某条件 是否包含于 某列中 ,简单点 就是:select * from 表名 where 字段名 like '%条件数据%'; 现在说下 某列 被包含于 条件数据中 接下 ...
- 二 python并发编程之多进程-理论
一 什么是进程 进程:正在进行的一个过程或者说一个任务.而负责执行任务则是cpu. 举例(单核+多道,实现多个进程的并发执行): egon在一个时间段内有很多任务要做:python备课的任务,写书的任 ...
- 用jq给img添加error事件
<img src="xxxx.jpg" alt="" /> <script> $(document).ready(function(){ ...
- php中foreach循环遍历二维数组
最近在用tp3.2框架,在查询的时候用到了select(),这条语句返回的是二维数组,所以在对返回的数据做处理时,遇到了些麻烦,百度了下foreach,终于用foreach解决了数据的筛选问题 (因为 ...