luogu 1993 小K的农场
差分约束+spfa判负环
dfs判负环
#include<bits/stdc++.h>
#define rep(i,x,y) for(register int i=x;i<=y;i++)
#define dec(i,x,y) for(register int i=x;i>=y;i--)
#define ll long long
using namespace std; const int N=;
const int inf=0x3f3f3f3f; int dis[N],vis[N],head[N],tot;
struct node{int v,w,next;}e[N];
void insert(int u,int v,int w){
e[++tot]=(node){v,w,head[u]};head[u]=tot;} inline int read(){
int x=,f=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-;ch=getchar();}
while(isdigit(ch)){x=(x<<)+(x<<)+(ch^);ch=getchar();}
return x*f;
}
int n,m,ch,a,b,c; inline int spfa(int u){
vis[u]=;
for(int i=head[u];i;i=e[i].next){
int v=e[i].v,w=e[i].w;
if(dis[v]<dis[u]+w){
dis[v]=dis[u]+w;
if(vis[v]) return ;
if(!spfa(v)) return ;
}
}
vis[u]=;
return ;
} int main(){
n=read();m=read();
while(m--){
ch=read();a=read();b=read();
if(ch==) c=read(),insert(b,a,c);
else if(ch==) c=read(),insert(a,b,-c);
else if(ch==) insert(a,b,),insert(b,a,);
}rep(i,,n) insert(,i,),dis[i]=-inf;
if(!spfa()) printf("No");
else printf("Yes");
return ;
}
luogu 1993 小K的农场的更多相关文章
- [Luogu] P1993 小K的农场
题目描述 小K在MC里面建立很多很多的农场,总共n个,以至于他自己都忘记了每个农场中种植作物的具体数量了,他只记得一些含糊的信息(共m个),以下列三种形式描述: 农场a比农场b至少多种植了c个单位的作 ...
- Luogu P1993 小 K 的农场
其实很早以前就打好了,但一直忘记写了. 也就是差分约束的模板题. 关于差分约束,也就是用来求关于一些不等式互相约束算出最优解. 推荐一个讲的很好的博客:http://www.cppblog.com/m ...
- 【luogu P1993 小K的农场】 题解
题目链接:https://www.luogu.org/problemnew/show/P1993 1.差分约束: 对于a - b <= c 有一条 b-->a 权值为c 对于a - b & ...
- 洛谷1993 小K的农场
原题链接 裸的差分约束. \(X_a-X_b\geqslant C\) \(X_a-X_b\leqslant C\Rightarrow X_b-X_a\geqslant -C\) \(X_a-X_b\ ...
- 洛谷P1993 小K的农场 [差分约束系统]
题目传送门 小K的农场 题目描述 小K在MC里面建立很多很多的农场,总共n个,以至于他自己都忘记了每个农场中种植作物的具体数量了,他只记得一些含糊的信息(共m个),以下列三种形式描述: 农场a比农场b ...
- 『题解』洛谷P1993 小K的农场
更好的阅读体验 Portal Portal1: Luogu Description 小\(K\)在\(\mathrm MC\)里面建立很多很多的农场,总共\(n\)个,以至于他自己都忘记了每个农场中种 ...
- 【BZOJ】3436: 小K的农场
3436: 小K的农场 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 938 Solved: 417[Submit][Status][Discuss ...
- 2014.7.7 模拟赛【小K的农场】
3.小K的农场(farm.pas/cpp/c) [题目描述] 小K在MC里面建立很多很多的农场,总共n个,以至于他自己都忘记了每个农场中种植作物的具体数量了,他只记得一些含糊的信息(共m个),以下列三 ...
- 【BZOJ3436】小K的农场(差分约束)
[BZOJ3436]小K的农场(差分约束) 题面 由于BZOJ巨慢无比,使用洛谷美滋滋 题解 傻逼差分约束题, 您要是不知道什么是差分约束 您就可以按下\(Ctrl+W\)了 #include< ...
随机推荐
- Angular 添加路由
var app=angular.module('kaifanla',['ng','ngRoute']);app.config(function($routeProvider){ //添加路由 $rou ...
- layer 中 的type和 content
type - 基本层类型 类型:Number,默认:0 layer提供了5种层类型.可传入的值有:0(信息框,默认)1(页面层)2(iframe层)3(加载层)4(tips层). 若你采用layer. ...
- Linux常用指令-ssh
目录 ssh远程登陆 ssh免密码登陆 生成公钥和私钥 将公钥复制到其他从机 文件说明 id_rsa id_rsa.pub authorized_keys known_host SSH(远程连接工具) ...
- checkStyle 错误普及
1Type is missing a javadoc commentClass 缺少类型说明 2“{” should be on the previous line“{” 应该位于前一行.解决方法: ...
- python---str和repr
在 Python 中要将某一类型的变量或者常量转换为字符串对象通常有两种方法,即 str() 或者 repr() . 区别与使用 函数str() 用于将值转化为适于人阅读的形式,而repr() 转化为 ...
- python系列-1 字符串操作
1.去除空格 str.strip():删除字符串两边的指定字符,括号的写入指定字符,默认为空格 >>> a=' hello ' >>> b=a.strip() ...
- linq 分组取各组最大值
static List<User> list1 = new List<User>() { new User(){id=1,name="张三"}, new U ...
- hdu 4897 Little Devil I (树链剖分+线段树)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4897 题意: 给你一棵树,一开始每条边都是白色,有三种操作: 1.将 u - v路径上的边转换颜色 ...
- codeforces 793B. Igor and his way to work
B. Igor and his way to work time limit per test 3 seconds memory limit per test 256 megabytes input ...
- 【CodeForces 624D/623B】Array GCD
题 You are given array ai of length n. You may consecutively apply two operations to this array: remo ...