zoukankan      html  css  js  c++  java
  • POJ 2983 Is the Information Reliable? 差分约束

    裸差分约束。

    //#pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<iostream>
    #include<sstream>
    #include<cmath>
    #include<climits>
    #include<string>
    #include<map>
    #include<queue>
    #include<vector>
    #include<stack>
    #include<set>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<int,int> pii;
    #define pb(a) push(a)
    #define INF 0x1f1f1f1f
    #define lson idx<<1,l,mid
    #define rson idx<<1|1,mid+1,r
    #define PI  3.1415926535898
    template<class T> T min(const T& a,const T& b,const T& c) {
        return min(min(a,b),min(a,c));
    }
    template<class T> T max(const T& a,const T& b,const T& c) {
        return max(max(a,b),max(a,c));
    }
    void debug() {
    #ifdef ONLINE_JUDGE
    #else
    
        freopen("data.in","r",stdin);
       // freopen("d:\out1.txt","w",stdout);
    #endif
    }
    int getch() {
        int ch;
        while((ch=getchar())!=EOF) {
            if(ch!=' '&&ch!='
    ')return ch;
        }
        return EOF;
    }
    
    struct Edge
    {
         int from, to, dist;
    };
    const int maxn = 1111;
    int n;
    vector<int> g[maxn];
    vector<Edge> edge;
    
    void Init()
    {
         for(int i = 1; i <= n; i++)
              g[i].clear();
         edge.clear();
    }
    void AddEdge(int u, int v, int w)
    {
         edge.push_back((Edge){u, v, w});
         g[u].push_back(edge.size() - 1);
    }
    
    int inq[maxn];
    int cnt[maxn];
    int d[maxn];
    
    bool SPFA()
    {
         queue<int> q;
         for(int i=1; i<=n; i++)
         {
              inq[i] = true;
              cnt[i] = 0;
              d[i] = 0;
              q.push(i);
         }
         while(!q.empty())
         {
              int u = q.front(); q.pop();
              inq[u] = false;
              for(int i=0; i < g[u].size(); i++)
              {
                   //cout<<g[u][i]<<endl;
                   Edge &e = edge[ g[u][i] ];
                   //cout<<" "<<e.from<<" "<<e.to<<" "<<e.dist<<endl;
                   if(d[e.to] < d[u] + e.dist)
                   {
                        d[e.to] = d[u] + e.dist;
                        inq[e.to] = true;
                        if(++cnt[e.to] > n) return 0;
                        q.push(e.to);
                   }
              }
         }
         return 1;
    }
    
    int main()
    {
         debug();
         int m;
         while(scanf("%d%d", &n, &m) != EOF)
         {
              Init();
              for(int i = 1; i <= m; i++)
              {
                   char op = getch();
                   if(op == 'P')
                   {
                        int u,v,x;
                        scanf("%d%d%d", &u, &v, &x);
                        AddEdge(u,v,x);
                        AddEdge(v,u,-x);
                   }else
                   {
                        int u,v;
                        scanf("%d%d", &u, &v);
                        AddEdge(v,u,1);
                   }
              }
              //cout<<edge.size()<<endl;
             // for(int i=0; i<=n; i++)
             //      cout<<g[i].size()<<endl;
              int ans = SPFA();
              printf("%s
    ", ans?"Reliable":"Unreliable");
         }
         return 0;
    }
    View Code
  • 相关阅读:
    Team Foundation Server(TFS)错误处理
    SET COMPATIBILITY_LEVEL的作用
    泛型:更好的匹配方法【翻译】
    jQuery min包的制作方法,压缩javascript的方法比对和方案[转载]
    Docker多平台架构镜像构建
    字符串字面值、C风格字符串、C++风格字符串
    启动异常tried to access method org.springframework.core.GenericTypeResolver.getTypeVariableMap
    py 之impala数据库连接
    py 之xlsxwriter 操作生成柱状图,饼图。
    SpringBoot学习遇到的问题及解决方案(持续更新)
  • 原文地址:https://www.cnblogs.com/BMan/p/3703505.html
Copyright © 2011-2022 走看看