King 差分约束 判负环
给出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 这样整个图就连起来了 否则是破碎的图 跑不动
注意连超级源点边的顺序
- #include<bits/stdc++.h>
- using namespace std;
- //input by bxd
- #define rep(i,a,b) for(int i=(a);i<=(b);i++)
- #define repp(i,a,b) for(int i=(a);i>=(b);--i)
- #define RI(n) scanf("%d",&(n))
- #define RII(n,m) scanf("%d%d",&n,&m)
- #define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
- #define RS(s) scanf("%s",s);
- #define ll long long
- #define REP(i,N) for(int i=0;i<(N);i++)
- #define CLR(A,v) memset(A,v,sizeof A)
- //////////////////////////////////
- #define inf 0x3f3f3f3f
- #define N 1000+5
- struct node
- {
- int to,nex,v;
- }edge[N];
- int pos=;
- int head[N];
- void add(int a,int b,int c)
- {
- edge[++pos].nex=head[a];
- head[a]=pos;
- edge[pos].v=c;
- edge[pos].to=b;
- }
- int n,m;
- int vis[N];
- int dis[N];
- int cnt[N];
- bool spfa()
- {
- queue<int>q;
- CLR(vis,);
- CLR(dis,0x3f);
- CLR(cnt,);
- q.push(n+);
- dis[n+]=;
- vis[n+]=;
- cnt[n+]=;
- while(!q.empty())
- {
- int u=q.front();q.pop();
- vis[u]=;
- for(int i=head[u];i;i=edge[i].nex)
- {
- int v=edge[i].to;
- if(dis[v]>dis[u]+edge[i].v)
- {
- dis[v]=dis[u]+edge[i].v;
- if(!vis[v])
- {
- cnt[v]++;
- if(cnt[v]>n)return ;
- q.push(v);
- vis[v]=;
- }
- }
- }
- }
- return ;
- }
- int main()
- {
- while(RI(n),n)
- {
- RI(m);
- CLR(head,);
- pos=;
- string str;
- int a,b,k;
- while(m--)
- {
- RII(a,b);cin>>str;RI(k);
- if(str=="gt")
- add(a+b,a-,-k-);
- else add(a-,a+b,k-);
- }
- rep(i,,n)
- add(n+,i,);//这里ab顺序反了也会错
- if(spfa())
- printf("lamentable kingdom\n");
- else printf("successful conspiracy\n");
- }
- return ;
- }
bellman算法判环更加简单不易错
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <algorithm>
- #include <iostream>
- #include <queue>
- #include <stack>
- #include <vector>
- #include <map>
- #include <set>
- #include <string>
- #include <math.h>
- using namespace std;
- #pragma warning(disable:4996)
- typedef long long LL;
- const int INF = <<;
- /*
- s[a+b+1] - s[a] >c --> s[a] - s[a+b+1] < -c <= -c-1
- s[a+b+1] - s[a] <c --> s[a+b+1] -s[a] < c <= c-1
- 不连通要我们求环
- */
- struct Edge
- {
- int from,to,dist;
- }es[+];
- int dist[+];
- bool bellman_ford(int n, int m)
- {
- //因为是求存不存在负权回路,那么只要初始化为0,更新n遍之后,如果还能再更新,那么就存在
- for(int i=; i<=n; ++i)
- dist[i] = ;
- for(int i=; i<=n; ++i)//n+1个点,所以要更新n遍
- {
- for(int j=; j<m; ++j)
- {
- Edge &e = es[j];
- //因为图是不连通的,所以如果置为INF的时候,不能在这里加这个条件
- if(/*dist[e.from]!=INF &&*/ dist[e.to] > dist[e.from] + e.dist)
- dist[e.to] = dist[e.from] + e.dist;
- }
- }
- for(int j=; j<m; ++j)
- {
- Edge &e = es[j];
- if(dist[e.to] > dist[e.from] + e.dist)
- return false;
- }
- return true;
- }
- int main()
- {
- int n,m,a,b,c,i;
- char str[];
- while(scanf("%d",&n),n)
- {
- scanf("%d",&m);
- for(i=; i<m; ++i)
- {
- scanf("%d%d%s%d",&a,&b,str,&c);
- if(str[]=='g')
- {
- es[i].from = a + b + ;
- es[i].to = a;
- es[i].dist = -c - ;
- }
- else
- {
- es[i].from = a;
- es[i].to = a + b +;
- es[i].dist = c-;
- }
- }
- bool ret = bellman_ford(n,m);
- if(ret)
- puts("lamentable kingdom");
- else
- puts("successful conspiracy");
- }
- return ;
- }
King 差分约束 判负环的更多相关文章
- 【10.9校内练习赛】【搜索】【2-sat】【树链剖分】【A_star k短路】【差分约束+判负环】
在洛谷上复制的题目! P3154 [CQOI2009]循环赛 题目描述 n队伍比赛,每两支队伍比赛一次,平1胜3负0. 给出队伍的最终得分,求多少种可能的分数表. 输入输出格式 输入格式: 第一行包含 ...
- Did Pong Lie? (差分系统 判负环)
Did Pong Lie? 时间限制: 5 Sec 内存限制: 128 MB提交: 68 解决: 15[提交][状态][讨论版] 题目描述 Doctor Pong has two arrays o ...
- poj 1364 King(线性差分约束+超级源点+spfa判负环)
King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14791 Accepted: 5226 Description ...
- POJ——1364King(差分约束SPFA判负环+前向星)
King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11946 Accepted: 4365 Description ...
- BZOJ.4500.矩阵(差分约束 SPFA判负环 / 带权并查集)
BZOJ 差分约束: 我是谁,差分约束是啥,这是哪 太真实了= = 插个广告:这里有差分约束详解. 记\(r_i\)为第\(i\)行整体加了多少的权值,\(c_i\)为第\(i\)列整体加了多少权值, ...
- POJ 1364 King --差分约束第一题
题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析 ...
- poj 3621 二分+spfa判负环
http://poj.org/problem?id=3621 求一个环的{点权和}除以{边权和},使得那个环在所有环中{点权和}除以{边权和}最大. 0/1整数划分问题 令在一个环里,点权为v[i], ...
- Poj(3259),SPFA,判负环
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- UVA 11090 Going in Cycle!!(二分答案+判负环)
在加权有向图中求平均权值最小的回路. 一上手没有思路,看到“回路”,第一想法就是找连通分量,可又是加权图,没什么好思路,那就转换题意:由求回路权值->判负环,求最小值->常用二分答案. 二 ...
随机推荐
- (二叉树 递归) leetcode94. Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
- SpringBoot入门:Hello World
1.Open IDEA,choose "New-->Project" 2.Choose "Spring Initializr" 3. Choose jav ...
- Go package(1) time 用法
golang使用的版本: go version go1.10.3 一:功能介绍 time的一些功能,比如时区,像linux中的定时器,时间计算等 格式化时间 时区(Location) 时间计算 Tic ...
- 计算机网络(HTTP)之客户识别:cookie机制
什么是cookie? 承载用户相关信息的HTTP首部 cookie的工作原理 cookie的缺陷 一.什么是cookie? cookie是由服务器生成,发送给USER-Agent(一般是浏览器),(服 ...
- shell 生成文件统计信息
#!/bin/bash #file name : filestat.sh if [ $# -ne 1 ]; then echo "Usage is $0 basepath"; ex ...
- python3抓图学习-百度贴吧
# coding=utf-8 from bs4 import BeautifulSoup import urllib.request import os import time def downlao ...
- mac下chrome 长截图(不使用插件)
1. command + option + i (打开windows下的f12): 2. command + shipt + p ; 3. 输入命令: Capture full size screen ...
- Codeforces Round #404 (Div. 2) D. Anton and School - 2
题目链接 转自 给你一个字符串问你能构造多少RSBS. #include<bits/stdc++.h> #define LL long long #define fi first #def ...
- HTML和XHTML区别
HTML和XHTML 可扩展超文本标记语言XHTML(eXtensible HyperText Markup Language)是将超文本标记语言HTML(HyperText Markup Langu ...
- 开源智能英文单词提取翻译工具(C#)
WordsTool 这个工具用于分析文本文件中所有的英语单词 并且通过内置字典数据库工具对这些单词进行解析 可以生成表格形式 并且支持导出到excel文件中 用于学习单词 本代码禁止商业用途 如需要商 ...