夏夜、

心若平似镜、何题不AC。

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!='\n')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\n", ans?"Reliable":"Unreliable");
     }
     return 0;
}
View Code

 

posted on 2014-05-01 23:52  BMan、  阅读(140)  评论(0编辑  收藏  举报

导航