题目大意:有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判断负权回路的方法,记录每个点进队次数,超过点的数目即存在负权回路。

代码:

  1. program infor;
  2. type
  3. point=^node;
  4. node=record
  5. x,len:longint; next:point;
  6. end;
  7. var
  8. a:array[..]of point;
  9. q:array[..]of longint;
  10. dis,vis:array[..]of longint;
  11. g:array[..]of boolean;
  12. n,i,m,x,y,v:longint; c:char; p:point;
  13. procedure put(x,y,v:longint);
  14. var p:point;
  15. begin
  16. new(p);
  17. p^.x:=x; p^.len:=v; p^.next:=a[y];a[y]:=p;
  18. end;
  19. procedure getnew;
  20. var i:longint;
  21. begin
  22. fillchar(q,sizeof(q),);
  23. fillchar(g,sizeof(g),false); fillchar(vis,sizeof(vis),);
  24. for i:= to n do dis[i]:=maxlongint div ;
  25. for i:= to n do begin dispose(a[i]); new(a[i]); a[i]:=nil; end;
  26. dis[]:=; g[]:=true; q[]:=;
  27. end;
  28. function spfa:boolean;
  29. var x,y,h,t:longint; p:point;
  30. begin
  31. h:=; t:=;
  32. while h<t do
  33. begin
  34. inc(h); x:=q[h]; g[x]:=false; new(p);p:=a[x];
  35. while p<>nil do
  36. begin
  37. y:=p^.x;
  38. if dis[x]+p^.len<dis[y] then
  39. begin
  40. dis[y]:=dis[x]+p^.len;
  41. if g[y]=false then
  42. begin
  43. inc(t); q[t]:=y; g[y]:=true;
  44. end;
  45. inc(vis[y]); if vis[y]>n then exit(false);
  46. end;
  47. p:=p^.next;
  48. end;
  49. end;
  50. exit(true);
  51. end;
  52. begin
  53. readln(n,m);
  54. while (n<>)or(m<>) do
  55. begin
  56. getnew;
  57. for i:= to n do put(i,,);
  58. for i:= to m do
  59. begin
  60. read(c);
  61. if c='P' then
  62. begin read(x,y,v); put(x,y,v); put(y,x,-v); end
  63. else if c='V' then begin read(x,y); put(y,x,-); end;
  64. if i<m then readln;
  65. end;
  66. if spfa then writeln('Reliable') else writeln('Unreliable');
  67. n:=; m:=;
  68. readln(n,m);
  69. end;
  70. end.

POJ 2983:Is the Information Reliable?(差分约束)的更多相关文章

  1. POJ 2983 Is the Information Reliable? 差分约束

    裸差分约束. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...

  2. POJ 2983 Is the Information Reliable? 依旧差分约束

    http://poj.org/problem?id=2983 题目大意: 星际大战开始了.你购买了情报,需要判断它的准确性.已知地方的根据地在由南向北排成一条直线.P A B X,表示A在B北面距离X ...

  3. POJ 2983 Is the Information Reliable? 信息可靠吗 (差分约束,spfa)

    题意:有n个站排成一列,针对每个站的位置与距离关系,现有多个约束条件,约束条件分两种:(1)确定的.明确说明站a距离站b多少个单位距离.(2)不确定的.只知道a在b的左边至少1个单位距离.  根据已知 ...

  4. POJ 2983 Is the Information Reliable?(差分约束系统)

    http://poj.org/problem?id=2983 题意:N 个 defense stations,M条消息,消息有精确和模糊之分,若前边为P.则为精确消息,有两个defense stati ...

  5. ●POJ 2983 Is the Information Reliable?

    题链: http://poj.org/problem?id=2983 题解: 差分约束. 1).对于条件(P u v w),不难发现反映到图上就是: $dis[u]-dis[v]=w$,所以添加两条边 ...

  6. POJ 2983-Is the Information Reliable?(差分约束系统)

    题目地址:POJ 2983 题意:有N个车站.给出一些点的精确信息和模糊信息.精确信息给出两点的位置和距离.模糊信息给出两点的位置.但距离大于等于一.试确定是否全部的信息满足条件. 思路:事实上就是让 ...

  7. POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)

    原题地址:http://poj.org/problem?id=3159 题意大概是班长发糖果,班里面有不良风气,A希望B的糖果不比自己多C个.班长要满足小朋友的需求,而且要让自己的糖果比snoopy的 ...

  8. POJ 之 Is the Information Reliable?

    B - Is the Information Reliable? Time Limit:3000MS     Memory Limit:131072KB     64bit IO Format:%I6 ...

  9. 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之间的 ...

  10. POJ 3159 Candies(spfa、差分约束)

    Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the he ...

随机推荐

  1. 如何解决EXCEL中的科学计数法

    EXCEL虽然能够有效的处理数据,尤其是数字的计算.但是,在单元格中输入数字的时候,很多时候都会受到科学计算法的困扰. 当单元格中输入的数字,超过11位时,就会自动变成科学计数法.无论您怎么调整列的宽 ...

  2. Linux 开启关闭防火墙

    开放防火墙端口添加需要监听的端口 /sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT/sbin/iptables -I INPUT -p tcp ...

  3. XML格式与实体类的转换

    背景 本人头一回写博客,请大家多多关照.通过读取XML文件获取用户管理权限,其中涉及三部分: 1.XML文件的生成: 2.XML文件的读取: 3.XML文件的保存: 如何做 第一步:自己先将XML文件 ...

  4. 【Python学习之八】ORM

    ORM 什么是ORM呢? ORM全称是:Object-Relational Mapping.即对象-关系映射,就是把关系数据库的一行映射为一个对象,也就是一个类对应一个表.这样,写代码更简单,不用直接 ...

  5. Python_循环判断表达式

    一.if判断 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. if判断结构: if 条件: 动作 elif 条件: 动作 else: 动作 if判断年龄: age_of_princal ...

  6. 如何在 Linux 中配置基于密钥认证的 SSH

    什么是基于 SSH 密钥的认证? 众所周知,Secure Shell,又称 SSH,是允许你通过无安全网络(例如 Internet)和远程系统之间安全访问/通信的加密网络协议.无论何时使用 SSH 在 ...

  7. 绘制矩形:描边矩形imagerectangle()、填充矩形imagefilledrectangle()

    <?php //1. 绘制图像资源(创建一个画布) $image = imagecreatetruecolor(500, 300); //2. 先分配一个绿色 $green = imagecol ...

  8. 5分钟带你快速理解Http协议

    HTTP协议 什么是HTTP协议 HTTP(Hyper Text Transfer Protocol)协议又叫超文本传输协议,是建立在TCP/IP协议之上的用来传递数据的协议.它不涉及数据包的传递,主 ...

  9. (ADO.NET)SqlCommand参数化查询

    string strcon = "Persist Security Info=False;User id=sa;pwd=lovemary;database=student;server=(l ...

  10. Hive 分析函数lead、lag实例应用

    Hive的分析函数又叫窗口函数,在oracle中就有这样的分析函数,主要用来做数据统计分析的. Lag和Lead分析函数可以在同一次查询中取出同一字段的前N行的数据(Lag)和后N行的数据(Lead) ...