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;
}

最新文章

  1. 利用Docker技术实现UDP广播效果(网络编程python版)
  2. php每天一题:strlen()与mb_strlen()的作用分别是什么
  3. wxPython入门练习代码 二
  4. split() 注意事项.
  5. unity3d c#调用控件属性
  6. cocos2d-x之首选项数据初试
  7. Interview---一道有趣的推理题
  8. 用 C# 在 Windows 7 中写注册表想到的
  9. Google前工程经理王忻:如何准备软件工程师的面试
  10. Java的递归算法
  11. CSS3 之 box-shadow
  12. Hadoop文件的基本操作
  13. Hadoop 它们的定义Writable NullpointerException
  14. 【Zookeeper】源码分析之请求处理链(三)
  15. JS的DOM操作及动画
  16. php中的四种排序算法
  17. 《HelloGitHub》第 21 期
  18. Xcode使用心得01:断点中断问题和调整编译目标
  19. ListView子项点击无反应的解决办法
  20. CentOS 7 安装JDK环境

热门文章

  1. 第九章 JQUI
  2. 错题分析--ASP.NET
  3. Django调用JS、CSS、图片等静态文件
  4. C# ASP.NET 优化程序性能、降低内存使用、提高程序运行速度
  5. git使用札记
  6. 通过实战理解C语言精要——函数篇
  7. 点击按钮后到底发生了什么,Touch,LongClick或者Click?
  8. Windows phone应用开发[18]-下拉刷新
  9. Sort using in VS
  10. 解决Centos/Redhat,命令不存在