http://poj.org/problem?id=2983

题意:给出M条信息,判断这些信息的正确性。(1)V A B :表示A,B之间的距离>=1; (2)P A B X :表示A B之间的距离为x。

思路:dis[i]表示i到原点的距离,由(1)知 dis[A]<=dis[B]-1 即B->A之间有一条边,权值为-1;由(2)知: dis[A]<=dis[B]-x && dis[B] <= dis[A]+x,即A->B的权值为-x,B->A的权值为x。增加一个超级源点,与所有的点相连且权值为0.建图,spfa判断是否有负环(即判断进站次数,若大于点数则存在负环)。若存在负环则信息为假,否则为真。

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <queue>
  4. using namespace std;
  5. const int N=;
  6. const int INF=<<;
  7. int head[],vis[];
  8. int dis[N],num[];
  9. int cnt,n;
  10.  
  11. struct node
  12. {
  13. int u,v,w;
  14. int next;
  15. } edge[N];
  16. void init()
  17. {
  18. cnt = ;
  19. memset(head,-,sizeof(head));
  20. memset(vis,,sizeof(vis));
  21. memset(num,,sizeof(num));
  22. }
  23. void add(int u,int v,int w)
  24. {
  25. edge[cnt].u = u;
  26. edge[cnt].v = v;
  27. edge[cnt].w = w;
  28. edge[cnt].next = head[u];
  29. head[u] = cnt++;
  30. }
  31. int spfa()
  32. {
  33. queue<int>q;
  34. for (int i = ; i <= n; i ++)
  35. dis[i] = INF;
  36. dis[] = ;
  37. q.push();
  38. vis[] = ;
  39. while(!q.empty())
  40. {
  41. int u = q.front();
  42. vis[u] = ;
  43. q.pop();
  44. for (int j = head[u]; j!=-; j=edge[j].next)
  45. {
  46. int v = edge[j].v;
  47. int w = edge[j].w;
  48. if (dis[v] > dis[u]+w)
  49. {
  50. dis[v] = dis[u]+w;
  51. if (!vis[v])
  52. {
  53. q.push(v);
  54. vis[v] = ;
  55. ++num[v];
  56. if (num[v] > n)
  57. return ;
  58. }
  59. }
  60. }
  61. }
  62. return ;
  63. }
  64. int main()
  65. {
  66. int m,u,v,w;
  67. char s[];
  68. while(~scanf("%d%d",&n,&m))
  69. {
  70. init();
  71. for (int i = ; i <= n; i++)
  72. add(,i,);
  73. for (int i = ; i < m; i++)
  74. {
  75. scanf("%s",s);
  76. if (s[]=='P')
  77. {
  78. scanf("%d %d %d",&u,&v,&w);
  79. add(u,v,w);
  80. add(v,u,-w);
  81. }
  82. if (s[]=='V')
  83. {
  84. scanf("%d %d",&u,&v);
  85. add(v,u,-);
  86. }
  87. }
  88. if (spfa())
  89. printf("Reliable\n");
  90. else
  91. printf("Unreliable\n");
  92. }
  93. return ;
  94. }

Is the Information Reliable?(差分约束系统)的更多相关文章

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

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

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

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

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

    id=2983">[POJ 2983]Is the Information Reliable? (差分约束系统) Is the Information Reliable? Time L ...

  4. poj2983--Is the Information Reliable?(差分约束)

    Is the Information Reliable? Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 11125   A ...

  5. Burn the Linked Camp(bellman 差分约束系统)

    Burn the Linked Camp Time Limit: 2 Seconds      Memory Limit: 65536 KB It is well known that, in the ...

  6. POJ2983 Is the Information Reliable?

    http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=267#problem/B                        B -  ...

  7. 【POJ 2983】 Is the information reliable?

    [题目链接] 点击打开链接 [算法] 差分约束系统,SPFA判负环 [代码] #include <algorithm> #include <bitset> #include & ...

  8. UVA11478 Halum [差分约束系统]

    https://vjudge.net/problem/UVA-11478 给定一个有向图,每条边都有一个权值.每次你可以选择一个结点v和一个整数d,把所有以v为终点的边的权值减小d,把所有以v为起点的 ...

  9. BZOJ 2330: [SCOI2011]糖果 [差分约束系统] 【学习笔记】

    2330: [SCOI2011]糖果 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 5395  Solved: 1750[Submit][Status ...

随机推荐

  1. UVA - 12325 Zombie's Treasure Chest (分类搜索)

    题目: 有一个体积为N的箱子和两种数量无限的宝物.宝物1的体积为S1,价值为V1:宝物2的体积为S2,价值为V2.输入均为32位带符号整数.计算最多能装多大价值的宝物,每种宝物都必须拿非负整数个. 思 ...

  2. Re0:DP学习之路 饭卡 HDU - 2546

    解法 01背包变式,首先贪心的想一下如果要保证余额最小那么就需要用相减后最小的钱减去之前最大的价格,且得保证这个钱在5元以上 对于寻找如何减最多能包含在5元以上,这里用01背包 我们把价钱看做体积装进 ...

  3. db2记录去重

    --查出二码,归档日期,借据号重复的数据的条数 select default_index_item_id,record_date,index_yxdk_dkjjh,min(sys_org_id),ma ...

  4. 关于ant引用android第三方工程打包的问题, invalid resource directory name: F:\workspace\Zlib\bin\res/crunch

    转载自 https://zhidao.baidu.com/question/200134399463655885.html invalid resource directory name: F:\wo ...

  5. ios 7以后 隐藏顶部状态栏

    iOS 7 以后,之前隐藏顶部状态栏的方法都已经失效.以下介绍的方法是全部兼容的. 修改xode工程里的 Info.plist 增加 Status bar is initially hidden一行, ...

  6. python——re模块(正则表达式)

    re 模块的使用: 1.使用compile()函数编译一个parttern对象, 例如:parttern=re.compile(r'\d+') 2.通过pattern对象提供的一系列属相和方法,对文本 ...

  7. A+B Problem IV

    描述acmj最近发现在使用计算器计算高精度的大数加法时很不方便,于是他想着能不能写个程序把这个问题给解决了.   输入 包含多组测试数据每组数据包含两个正数A,B(可能为小数且位数不大于400) 输出 ...

  8. HDU1914(稳定婚姻)

    http://acm.hdu.edu.cn/showproblem.php?pid=1914 思路:Gale-Shapley算法.算法过程是男士不停地求婚,女士不停地拒绝.在每一轮中,每个尚未订婚的男 ...

  9. JAVA内存模型与线程以及volatile理解

    Java内存模型是围绕在并发过程中如何处理原子性.可见性.有序性来建立的. 一.主内存与工作内存 Java内存模型主要目标是在虚拟机中将变量存储到内存和从内存中取出变量.这里的变量包括:实例字段.静态 ...

  10. Java获取系统环境变量(System Environment Variable)和系统属性(System Properties)以及启动参数的方法

    系统环境变量(System Environment Variable): 在Linux下使用export $ENV=123指定的值.获取的方式如下: Map<String,String> ...