给出n个不等式

给出四个参数第一个数i可以代表序列的第几项,然后给出n,这样前面两个数就可以描述为ai+a(i+1)+...a(i+n),即从i到n的连续和,再给出一个符号和一个ki
当符号为gt代表‘>’,符号为lt代表‘<'
那么样例可以表示
1 2 gt 0
a1+a2+a3>0
2 2 lt 2
a2+a3+a4<2

求是否存在不等式使得ai+a(i+1)+a(i+2)+...+a(i+n)<ki或者是ai+a(i+1)+a(i+2)+...+a(i+n)>ki 成立

显然就是一个判断是否存在负环

差分约束中

如果存在负环 说明无解  不存在

如果断路 无限解

spfa

用spfa算法还需要设置一个超级源点n+1  和所有边相连 距离为0  这样整个图就连起来了  否则是破碎的图 跑不动

注意连超级源点边的顺序

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. //input by bxd
  4. #define rep(i,a,b) for(int i=(a);i<=(b);i++)
  5. #define repp(i,a,b) for(int i=(a);i>=(b);--i)
  6. #define RI(n) scanf("%d",&(n))
  7. #define RII(n,m) scanf("%d%d",&n,&m)
  8. #define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
  9. #define RS(s) scanf("%s",s);
  10. #define ll long long
  11. #define REP(i,N) for(int i=0;i<(N);i++)
  12. #define CLR(A,v) memset(A,v,sizeof A)
  13. //////////////////////////////////
  14. #define inf 0x3f3f3f3f
  15. #define N 1000+5
  16.  
  17. struct node
  18. {
  19. int to,nex,v;
  20. }edge[N];
  21. int pos=;
  22. int head[N];
  23. void add(int a,int b,int c)
  24. {
  25. edge[++pos].nex=head[a];
  26. head[a]=pos;
  27. edge[pos].v=c;
  28. edge[pos].to=b;
  29. }
  30. int n,m;
  31. int vis[N];
  32. int dis[N];
  33. int cnt[N];
  34. bool spfa()
  35. {
  36. queue<int>q;
  37. CLR(vis,);
  38. CLR(dis,0x3f);
  39. CLR(cnt,);
  40. q.push(n+);
  41. dis[n+]=;
  42. vis[n+]=;
  43. cnt[n+]=;
  44. while(!q.empty())
  45. {
  46. int u=q.front();q.pop();
  47. vis[u]=;
  48. for(int i=head[u];i;i=edge[i].nex)
  49. {
  50. int v=edge[i].to;
  51. if(dis[v]>dis[u]+edge[i].v)
  52. {
  53. dis[v]=dis[u]+edge[i].v;
  54. if(!vis[v])
  55. {
  56. cnt[v]++;
  57. if(cnt[v]>n)return ;
  58. q.push(v);
  59. vis[v]=;
  60. }
  61. }
  62. }
  63. }
  64. return ;
  65. }
  66. int main()
  67. {
  68. while(RI(n),n)
  69. {
  70. RI(m);
  71. CLR(head,);
  72. pos=;
  73. string str;
  74. int a,b,k;
  75. while(m--)
  76. {
  77. RII(a,b);cin>>str;RI(k);
  78. if(str=="gt")
  79. add(a+b,a-,-k-);
  80. else add(a-,a+b,k-);
  81. }
  82. rep(i,,n)
  83. add(n+,i,);//这里ab顺序反了也会错
  84. if(spfa())
  85. printf("lamentable kingdom\n");
  86. else printf("successful conspiracy\n");
  87. }
  88. return ;
  89. }

bellman算法判环更加简单不易错

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <algorithm>
  5. #include <iostream>
  6. #include <queue>
  7. #include <stack>
  8. #include <vector>
  9. #include <map>
  10. #include <set>
  11. #include <string>
  12. #include <math.h>
  13. using namespace std;
  14. #pragma warning(disable:4996)
  15. typedef long long LL;
  16. const int INF = <<;
  17. /*
  18. s[a+b+1] - s[a] >c --> s[a] - s[a+b+1] < -c <= -c-1
  19. s[a+b+1] - s[a] <c --> s[a+b+1] -s[a] < c <= c-1
  20. 不连通要我们求环
  21. */
  22. struct Edge
  23. {
  24. int from,to,dist;
  25. }es[+];
  26. int dist[+];
  27. bool bellman_ford(int n, int m)
  28. {
  29. //因为是求存不存在负权回路,那么只要初始化为0,更新n遍之后,如果还能再更新,那么就存在
  30. for(int i=; i<=n; ++i)
  31. dist[i] = ;
  32. for(int i=; i<=n; ++i)//n+1个点,所以要更新n遍
  33. {
  34. for(int j=; j<m; ++j)
  35. {
  36. Edge &e = es[j];
  37. //因为图是不连通的,所以如果置为INF的时候,不能在这里加这个条件
  38. if(/*dist[e.from]!=INF &&*/ dist[e.to] > dist[e.from] + e.dist)
  39. dist[e.to] = dist[e.from] + e.dist;
  40. }
  41. }
  42. for(int j=; j<m; ++j)
  43. {
  44. Edge &e = es[j];
  45. if(dist[e.to] > dist[e.from] + e.dist)
  46. return false;
  47. }
  48. return true;
  49. }
  50. int main()
  51. {
  52.  
  53. int n,m,a,b,c,i;
  54. char str[];
  55. while(scanf("%d",&n),n)
  56. {
  57. scanf("%d",&m);
  58. for(i=; i<m; ++i)
  59. {
  60. scanf("%d%d%s%d",&a,&b,str,&c);
  61. if(str[]=='g')
  62. {
  63. es[i].from = a + b + ;
  64. es[i].to = a;
  65. es[i].dist = -c - ;
  66. }
  67. else
  68. {
  69. es[i].from = a;
  70. es[i].to = a + b +;
  71. es[i].dist = c-;
  72. }
  73. }
  74. bool ret = bellman_ford(n,m);
  75. if(ret)
  76. puts("lamentable kingdom");
  77. else
  78. puts("successful conspiracy");
  79. }
  80. return ;
  81. }

King 差分约束 判负环的更多相关文章

  1. 【10.9校内练习赛】【搜索】【2-sat】【树链剖分】【A_star k短路】【差分约束+判负环】

    在洛谷上复制的题目! P3154 [CQOI2009]循环赛 题目描述 n队伍比赛,每两支队伍比赛一次,平1胜3负0. 给出队伍的最终得分,求多少种可能的分数表. 输入输出格式 输入格式: 第一行包含 ...

  2. Did Pong Lie? (差分系统 判负环)

    Did Pong Lie? 时间限制: 5 Sec  内存限制: 128 MB提交: 68  解决: 15[提交][状态][讨论版] 题目描述 Doctor Pong has two arrays o ...

  3. poj 1364 King(线性差分约束+超级源点+spfa判负环)

    King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14791   Accepted: 5226 Description ...

  4. POJ——1364King(差分约束SPFA判负环+前向星)

    King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11946   Accepted: 4365 Description ...

  5. BZOJ.4500.矩阵(差分约束 SPFA判负环 / 带权并查集)

    BZOJ 差分约束: 我是谁,差分约束是啥,这是哪 太真实了= = 插个广告:这里有差分约束详解. 记\(r_i\)为第\(i\)行整体加了多少的权值,\(c_i\)为第\(i\)列整体加了多少权值, ...

  6. POJ 1364 King --差分约束第一题

    题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析 ...

  7. poj 3621 二分+spfa判负环

    http://poj.org/problem?id=3621 求一个环的{点权和}除以{边权和},使得那个环在所有环中{点权和}除以{边权和}最大. 0/1整数划分问题 令在一个环里,点权为v[i], ...

  8. Poj(3259),SPFA,判负环

    题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  9. UVA 11090 Going in Cycle!!(二分答案+判负环)

    在加权有向图中求平均权值最小的回路. 一上手没有思路,看到“回路”,第一想法就是找连通分量,可又是加权图,没什么好思路,那就转换题意:由求回路权值->判负环,求最小值->常用二分答案. 二 ...

随机推荐

  1. (二叉树 递归) leetcode94. Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...

  2. SpringBoot入门:Hello World

    1.Open IDEA,choose "New-->Project" 2.Choose "Spring Initializr" 3. Choose jav ...

  3. Go package(1) time 用法

    golang使用的版本: go version go1.10.3 一:功能介绍 time的一些功能,比如时区,像linux中的定时器,时间计算等 格式化时间 时区(Location) 时间计算 Tic ...

  4. 计算机网络(HTTP)之客户识别:cookie机制

    什么是cookie? 承载用户相关信息的HTTP首部 cookie的工作原理 cookie的缺陷 一.什么是cookie? cookie是由服务器生成,发送给USER-Agent(一般是浏览器),(服 ...

  5. shell 生成文件统计信息

    #!/bin/bash #file name : filestat.sh if [ $# -ne 1 ]; then echo "Usage is $0 basepath"; ex ...

  6. python3抓图学习-百度贴吧

    # coding=utf-8 from bs4 import BeautifulSoup import urllib.request import os import time def downlao ...

  7. mac下chrome 长截图(不使用插件)

    1. command + option + i (打开windows下的f12): 2. command + shipt + p ; 3. 输入命令: Capture full size screen ...

  8. Codeforces Round #404 (Div. 2) D. Anton and School - 2

    题目链接 转自 给你一个字符串问你能构造多少RSBS. #include<bits/stdc++.h> #define LL long long #define fi first #def ...

  9. HTML和XHTML区别

    HTML和XHTML 可扩展超文本标记语言XHTML(eXtensible HyperText Markup Language)是将超文本标记语言HTML(HyperText Markup Langu ...

  10. 开源智能英文单词提取翻译工具(C#)

    WordsTool 这个工具用于分析文本文件中所有的英语单词 并且通过内置字典数据库工具对这些单词进行解析 可以生成表格形式 并且支持导出到excel文件中 用于学习单词 本代码禁止商业用途 如需要商 ...