zoukankan      html  css  js  c++  java
  • BZOJ 3436: 小K的农场 差分约束

    3436: 小K的农场

    Time Limit: 1 Sec  

    Memory Limit: 256 MB

    题目连接

    http://www.lydsy.com/JudgeOnline/problem.php?id=3436

    Description

     背景

        小K是个特么喜欢玩MC的孩纸。。。

     描述

        小K在MC里面建立很多很多的农场,总共n个,以至于他自己都忘记了每个农场中种植作物的具体数量了,他只记得一些含糊的信息(共m个),以下列三种形式描述:农场a比农场b至少多种植了c个单位的作物,农场a比农场b至多多种植了c个单位的作物,农场a与农场b种植的作物数一样多。但是,由于小K的记忆有些偏差,所以他想要知道存不存在一种情况,使得农场的种植作物数量与他记忆中的所有信息吻合。输入格式

    Input

       第一行包括两个整数n和m,分别表示农场数目和小K记忆中的信息的数目

        接下来m行:

        如果每行的第一个数是1,接下来有三个整数a,b,c,表示农场a比农场b至少多种植了c个单位的作物    如果每行第一个数是2,接下来有三个整数a,b,c,表示农场a比农场b至多多种植了c个单位的作物

        如果每行第一个数是3,接下来有两个整数a,b,表示农场a种植的数量与b一样多输出格式

    Output

    如果存在某种情况与小K的记忆吻合,输出”Yes”,否则输出”No”

    Sample Input

    33
    312
    1131
    2232

    Sample Output

    Yes

    HINT

    1<=n,m,a,b,c<=10000

    题意

    题解:

    差分约束

    找时候存在负环就好了

    代码:

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <bitset>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 11000
    #define mod 10007
    #define eps 1e-9
    int Num;
    //const int inf=0x7fffffff;   //§ß§é§à§é¨f§³
    const int inf=0x3f3f3f3f;
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    //**************************************************************************************
    struct node
    {
        int x,y;
    };
    vector<node> E[maxn];
    int inq[maxn],dis[maxn];
    int flag=0;
    void solve(int x)
    {
        if(flag)
            return;
        inq[x]=1;
        for(int i=0;i<E[x].size();i++)
        {
            node v = E[x][i];
            if(dis[v.x]>dis[x]+v.y)
            {
                dis[v.x]=dis[x]+v.y;
                if(inq[v.x])
                {
                    flag=1;
                    return;
                }
                if(!inq[v.x])
                {
                    dis[v.x]=dis[x]+v.y;
                    solve(v.x);
                }
            }
        }
        inq[x]=0;
    }
    int main()
    {
        int n=read(),m=read();
        for(int i=0;i<=n;i++)
            dis[i]=inf;
        for(int i=1;i<=m;i++)
        {
            int a=read(),b=read(),c=read();
            if(a==1)
            {
                int e=read();
                E[b].push_back((node){c,-e});
            }
            if(a==2)
            {
                int e=read();
                E[c].push_back((node){b,e});
            }
            if(a==3)
            {
                E[b].push_back((node){c,0});
            }
        }
        for(int i=1;i<=n;i++)
            dis[i]=0,solve(i);
        if(flag)printf("No
    ");
        else printf("Yes
    ");
    }
  • 相关阅读:
    Exection throws和throw
    Exection(二)
    JAVA-Exception
    IDEA初见---输出HelloWorld,并打包成jar包
    Centos6.5安装Tomcat
    Centos6.5下装ZooKeeper
    LInux下装jdk
    python作业高级FTP
    tcp黏包
    计算器
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4776586.html
Copyright © 2011-2022 走看看