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!!(二分答案+判负环)
在加权有向图中求平均权值最小的回路. 一上手没有思路,看到“回路”,第一想法就是找连通分量,可又是加权图,没什么好思路,那就转换题意:由求回路权值->判负环,求最小值->常用二分答案. 二 ...
随机推荐
- (模拟) codeVs1160 蛇形矩阵
题目描述 Description 小明玩一个数字游戏,取个n行n列数字矩阵(其中n为不超过100的奇数),数字的填补方法为:在矩阵中心从1开始以逆时针方向绕行,逐圈扩大,直到n行n列填满数字,请输出该 ...
- Centos6安装Percona-tools工具
Centos6安装Percona-tools工具 环境:centos6.x yum -y install perl-DBI yum -y install perl-DBD-MySQL yum -y i ...
- (Python3) 九九乘法表 代码
for i in range(1,10): for j in range(1,10): print(i '*' j '=', i*j)
- 移动端1px问题处理方法
在做移动端开发时,设计师提供的视觉稿一般是750px,当你定义 border-width:1px 时,在iphone6手机上却发现:边框变粗了.. 这是因为,1px是相对于750px的(物理像素),而 ...
- webpack之proxyTable设置跨域
可以看看这个视频中的介绍 :https://ke.qq.com/course/350693?tuin=f158ed6
- 用SQL表达交并差操作
交-并-差的处理 SQL语言:并运算UNION,交运算INTERSECT,差运算EXCEPT 基本语法形式: 子查询{UNION [ALL] | INTERSECT [ALL] | EXPECT [A ...
- webstorm快速输入标签
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 优化算法:AdaGrad | RMSProp | AdaDelta | Adam
0 - 引入 简单的梯度下降等优化算法存在一个问题:目标函数自变量的每一个元素在相同时间步都使用同一个学习率来迭代,如果存在如下图的情况(不同自变量的梯度值有较大差别时候),存在如下问题: 选择较小的 ...
- Css - 选择器和样式
Css - 选择器和样式 标签选择器 即使用html标签作为选择对象 <style> div{ background:red; } </style> <div&g ...
- 使用 “mini-css-extract-plugin” 提取css到单独的文件
一.前言 我们在使用webpack构建工具的时候,通过style-loader,可以把解析出来的css通过js插入内部样式表的方式到页面中,插入的结果如下: <style> .wrappe ...