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

思路:

  第2种条件比较好确定,如果知道如何用最短路解差分约束的话。

  问题在第1种,明确地说明了距离,怎么办?拆成两条式子,比如 dis(a,b)=c,那么可以写成 b-a>=c ,b-a<=c 这样,只要满足这两个条件,原来明确说明的距离也会成立的。这样就可以根据两条式子建图了。再用spfa解就可以了。

  1. //#include <bits/stdc++.h>
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <vector>
  5. #include <cstring>
  6. #include <deque>
  7. #define INF 0x7f7f7f7f
  8. #define pii pair<int,int>
  9. #define LL unsigned long long
  10. using namespace std;
  11. const int N=;
  12. struct node
  13. {
  14. int from, to, cost;
  15. node(){};
  16. node(int from,int to,int cost):from(from),to(to),cost(cost){};
  17. }edge[N*N];
  18. int edge_cnt;
  19. vector<int> vect[N];
  20.  
  21. void add_node(int from,int to,int cost)
  22. {
  23. edge[edge_cnt]=node(from, to, cost);
  24. vect[from].push_back(edge_cnt++);
  25. }
  26.  
  27. int inq[N], cost[N], cnt[N];
  28.  
  29. bool spfa(int n)
  30. {
  31. memset(inq, , sizeof(inq));
  32. memset(cost, , sizeof(cost));
  33. memset(cnt, , sizeof(cnt));
  34. deque<int> que;
  35. for(int i=; i<=n; i++) que.push_back(i);
  36.  
  37. while(!que.empty())
  38. {
  39. int x=que.front();
  40. que.pop_front();
  41. inq[x]=;
  42. for(int i=; i<vect[x].size(); i++)
  43. {
  44. node e=edge[vect[x][i]];
  45. if(cost[e.to]>cost[e.from]+e.cost)
  46. {
  47. cost[e.to]=cost[e.from]+e.cost;
  48. if(!inq[e.to])
  49. {
  50. inq[e.to]=;
  51. if(++cnt[e.to]>n) return false;
  52. if(!que.empty()&& cost[e.to]<cost[que.front()])
  53. que.push_front(e.to);
  54. else
  55. que.push_back(e.to);
  56. }
  57. }
  58. }
  59. }
  60. return true;
  61. }
  62.  
  63. int main()
  64. {
  65. freopen("input.txt", "r", stdin);
  66. int t, a, b, d, n, m;
  67. char c;
  68. while(~scanf("%d%d", &n, &m))
  69. {
  70. edge_cnt=;
  71. for(int i=; i<=n; i++) vect[i].clear();
  72. memset(edge,,sizeof(edge));
  73.  
  74. for(int i=; i<m; i++)
  75. {
  76. while(scanf("%c", &c), !isalpha(c) );
  77. if(c=='P')//确定的,要拆
  78. {
  79. scanf("%d %d %d", &a, &b, &d);
  80. add_node(a,b,d);
  81. add_node(b,a,-d);
  82. }
  83. else
  84. {
  85. scanf("%d %d", &a, &b);
  86. add_node(b,a,-);
  87. }
  88. }
  89. if(spfa(n)) puts("Reliable");
  90. else puts("Unreliable");
  91. }
  92. return ;
  93. }

AC代码

POJ 2983 Is the Information Reliable? 信息可靠吗 (差分约束,spfa)的更多相关文章

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

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

  2. ●POJ 2983 Is the Information Reliable?

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

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

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

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

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

  5. POJ 1201 &amp; HDU1384 &amp; ZOJ 1508 Intervals(差分约束+spfa 求最长路径)

    题目链接: POJ:http://poj.org/problem?id=1201 HDU:http://acm.hdu.edu.cn/showproblem.php? pid=1384 ZOJ:htt ...

  6. POJ 之 Is the Information Reliable?

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

  7. poj Layout 差分约束+SPFA

    题目链接:http://poj.org/problem?id=3169 很好的差分约束入门题目,自己刚看时学呢 代码: #include<iostream> #include<cst ...

  8. POJ 1364 / HDU 3666 【差分约束-SPFA】

    POJ 1364 题解:最短路式子:d[v]<=d[u]+w 式子1:sum[a+b+1]−sum[a]>c      —      sum[a]<=sum[a+b+1]−c−1  ...

  9. POJ 3159 Candies(差分约束+spfa+链式前向星)

    题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A ...

随机推荐

  1. Python中编码问题?

    一.键盘输入 raw_input('请输入:'.decode('utf-8').encode('gbk'))raw_input(unicode('请输入:','utf-8').encode('gbk' ...

  2. HDU4782 Beautiful Soup

    成都赛里的一道坑爹码力题,突然间脑抽想做一下弥补一下当时的遗憾.当时没做出这道题一是因为当时只剩大概45分钟,对于这样的具有各种条件的题无从下手,二则是因为当时估算着已经有银牌了,所以就不挣扎了.但是 ...

  3. iOS生成本地随机验证码

    原文链接:http://www.cnblogs.com/jerehedu/p/4527707.html 效果图:

  4. 怎样强制QQ聊天

    首先复制下面这段网址: http://wp.qq.com/open_webaio.html?sigt=2d3bb7d31517da8c94a1061c6b63dd3203eb633805dcd09ec ...

  5. ***Xcode Interface Builder或Storyboard中可建立那两种连接?

    在Xcode Interface Builder或Storyboard中,可建立到输出口(IBOutlet)和操作(方法,IBAction)的连接. IBOutlet are for output C ...

  6. Codeforces Round #336 (Div. 2) D. Zuma 区间dp

    D. Zuma   Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gems ...

  7. lintcode:背包问题

    背包问题 在n个物品中挑选若干物品装入背包,最多能装多满?假设背包的大小为m,每个物品的大小为A[i] 样例 如果有4个物品[2, 3, 5, 7] 如果背包的大小为,可以选择的空间. 如果背包的大小 ...

  8. Android 核心分析之十三Android GWES之Android窗口管理

    Android GWES之Android窗口管理1基本构架原理 Android的窗口管理是C/S模式的.Android中的Window是表示Top Level等顶级窗口的概念.DecorView是Wi ...

  9. mysql C api

    1.初始化一个链接结构. 2.创建一个链接. 3.执行查询. 4.关闭链接. MYSQL* conn; 首先,声明一个conn指针指向一个MYSQL结构体,这个结构体就是一个数据库连接句柄. conn ...

  10. Linux SHELL脚本

    在Linux系统中,虽然有各种各样的图形化接口工具,但是sell仍然是一个非常灵活的工具.Shell不仅仅是命令的收集,而且是一门非常棒的编程语言.可以通过使用shell使大量的任务自动化,shell ...