POJ 2983 Is the Information Reliable?(差分约束#7)

#include <cstdio>
#include <algorithm>
#include <cstdlib> 
#include <cstring>
#define N 1010 
#define M 1000000
#define MAXQ  1000000
using namespace std; 
const int inf = (-1u >> 1); 

int d[N], first[N];
int u[M], v[M] , w[M], next[M]; 
bool inq[N];
int cnt[N]; 
int e;
int q[MAXQ];

void addE(int x, int y, int c)
{
      next[e] = first[x], first[x] = e;
      u[e] = x, v[e] = y;
      w[e] = c; 
      e++;
}

bool SPFA(int s, int n)
{
       for (int i = 0; i <= n; i++)d[i] = inf;
       memset(cnt, 0, sizeof(cnt)); 
       d[s] = 0;
       int qs, qe; 
       qs = qe = 0; 
       for (int i = 0; i <= n; i++)inq[i] = false; 
       q[qe++] = s; 
       while (qs != qe)
       {
              int x = q[qs]; 
              inq[x] = false;
              for (int e = first[x]; e != -1; e = next[e])if (d[v[e]] > d[u[e]] + w[e])
              {
                    d[v[e]] = d[u[e]] + w[e]; 
                    if (!inq[v[e]])
                    {
                              inq[v[e]] = true;
                              q[qe++] = v[e];
                              if (qe == MAXQ)qe = 0; 
                              if (++cnt[v[e]] >n)return false; 
                    }
              }
              qs++;
              if (qs == MAXQ)qs = 0; 
       }
      return true; 
} 

int main()
{
      int n, m;
     // FILE* fp = fopen("in.txt", "r");  
      while (scanf(  "%d", &n) != EOF)
      {
             scanf( "%d", &m);
             int a, b, c;
             char tt[3]; 
             e = 0; 
             for (int i = 0; i <= n; i++)first[i] = -1; 
             for (int i = 0; i < m; i++) 
             { 
                  scanf( "%s %d %d", tt, &a, &b);
                  if (tt[0] == 'V')
                       addE(a, b, -1);
                  else {
                       scanf( "%d", &c);
                       addE(b, a, c);
                       addE(a, b, -c);
                  }     
             }
             for (int i = 1; i <= n; i++)addE(0, i, 0); 
                      
             
             if (SPFA(0, n))puts("Reliable"); 
             else puts("Unreliable");
          //   getchar();getchar(); 
      }
      return 0;
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值