poj 2983 (差分约束系统)

       题目就是给你一组由等式和不等式组成的式子,让你判断这组式子是否有解。为了使用差分约束系统,将等式a-b=c转化为a-b>=c和a-b<=c。然后就是构图判断是否有负环就行了。

      代码如下:

#include <cstdio>
#include <stack>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <iomanip>
#include <cmath>
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(arr, num) memset(arr, num, sizeof(arr))
#define PB push_back
#define F first
#define S second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid + 1, r
#define LRT rt << 1
#define RRT rt << 1|1
#define BitCount(x) __builtin_popcount(x)
#define BitCountll(x) __builtin_popcountll(x)
#define LeftPos(x) 32 - __builtin_clz(x) - 1
#define LeftPosll(x) 64 - __builtin_clzll(x) - 1
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
using namespace std;
const double eps = 1e-8;
const int MAXN = 300 + 10;
const int MOD = 1000007;
const int M=20010;
const int N=300010;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
typedef pair<int, int> pii;
int n,m,r,d[N];
int w[N],u[N],v[N],first[N],next[N];
bool vis[N];
bool bellman()
{
    int i,j;
    for (i=1;i<=n;d[i++]=INF);
    d[1]=0;
    bool flag=false;
    for (i=0;i<n;i++){
        flag=false;
        for (j=0;j<m;j++){
            if (d[v[j]]>d[u[j]]+w[j]){
                d[v[j]]=d[u[j]]+w[j];
                flag=true;
            }
        }
        if (!flag) return true; // 在一次循环里对所有边都没有更新,说明没有负环
    }
    if (flag) return false; // 有负环
    else return true;  // 没有负环
}
int main()
{
    int i,j;
    while(~scanf("%d%d",&n,&m))
    {
        char s[N];
        int a,b,c;
        for (i=0,j=0;i<m;i++){
            scanf("%s",s);
            if (s[0]=='P') {
                scanf("%d%d%d",&a,&b,&c);
                u[j]=a; v[j]=b; w[j++]=c;
                u[j]=b; v[j]=a; w[j++]=-c;
            }
            else {
                scanf("%d%d",&a,&b);
                u[j]=b; v[j]=a; w[j++]=-1;
            }
        }
        m=j;
        if (bellman()) puts("Reliable");
        else puts("Unreliable");
    }
}























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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值