码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2983 Is the Information Reliable? 差分约束

时间:2014-05-02 13:13:29      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   tar   

裸差分约束。

bubuko.com,布布扣
bubuko.com,布布扣
//#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
bubuko.com,布布扣

 

POJ 2983 Is the Information Reliable? 差分约束,布布扣,bubuko.com

POJ 2983 Is the Information Reliable? 差分约束

标签:style   blog   class   code   java   tar   

原文地址:http://www.cnblogs.com/BMan/p/3703505.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!