BZOJ3436——小K的农场
2024-10-14 18:41:30
1、题意:大概是给一些制约限制,问是否存在合法解
2、分析:我们来观察这三个限制
农场a比农场b至少多种植了c个单位的作物 可以变成b 比 a至多多种了-c
农场a比农场b至多多种植了c个单位的作物 可以变成a 比
b至多多种了c
农场a与农场b种植的作物数一样多
就是a = b
那么利用差分约束我们可以把这个东西转为一个图,那么如果这个图中有正环那么这个图就是不合法的,如何判断有没有正环呢?
我们可以利用spfa求最短路时判负环,我们把它改成求最长路时,判正环,然后就AC了
#include <queue> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> using namespace std; #define M 1000010 #define inf 1047483647 inline int read(){ char ch = getchar(); int x = 0, f = 1; while(ch < '0' || ch > '9'){ if(ch == '-') f = -1; ch = getchar(); } while('0' <= ch && ch <= '9'){ x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } struct Edge{ int u, v, w, next; } G[M]; int head[M], tot; int inq[M], d[M]; int n, m; inline void add(int u, int v, int w){ G[++ tot] = (Edge){u, v, w, head[u]}; head[u] = tot; } inline bool spfa(){ for(int i = 1; i <= n; i ++) d[i] = -inf; queue<int> Q; Q.push(0); while(!Q.empty()){ int x = Q.front(); Q.pop(); inq[x] = 0; for(int i = head[x]; i != -1; i = G[i].next){ if(d[G[i].v] < d[x] + G[i].w){ if(inq[G[i].v]){ return false; } d[G[i].v] = d[x] + G[i].w; Q.push(G[i].v); inq[G[i].v] = 1; } } } return true; } int main(){ n = read(), m = read(); memset(head, -1, sizeof(head)); for(int i = 1; i <= m; i ++){ int op = read(); if(op == 2){ int a = read(), b = read(), c = read(); add(a, b, c); } else if(op == 1){ int a = read(), b = read(), c = read(); add(b, a, -c); } else{ int a = read(), b = read(); add(a, b, 0); add(b, a, 0); } } for(int i = 1; i <= n; i ++) add(0, i, 0); if(spfa()) puts("Yes"); else puts("No"); return 0; }
最新文章
- 利用Docker技术实现UDP广播效果(网络编程python版)
- php每天一题:strlen()与mb_strlen()的作用分别是什么
- wxPython入门练习代码 二
- split() 注意事项.
- unity3d c#调用控件属性
- cocos2d-x之首选项数据初试
- Interview---一道有趣的推理题
- 用 C# 在 Windows 7 中写注册表想到的
- Google前工程经理王忻:如何准备软件工程师的面试
- Java的递归算法
- CSS3 之 box-shadow
- Hadoop文件的基本操作
- Hadoop 它们的定义Writable NullpointerException
- 【Zookeeper】源码分析之请求处理链(三)
- JS的DOM操作及动画
- php中的四种排序算法
- 《HelloGitHub》第 21 期
- Xcode使用心得01:断点中断问题和调整编译目标
- ListView子项点击无反应的解决办法
- CentOS 7 安装JDK环境