POJ 2983 差分约束系统

本文介绍了一种通过Bellman-Ford算法验证战争中获取信息可靠性的方法。具体来说,算法处理了精确距离和模糊距离两种类型的信息提示,并通过构建图模型来判断这些提示是否存在矛盾。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

Is the Information Reliable?
Time Limit: 3000MS Memory Limit: 131072K
Total Submissions: 12490 Accepted: 3930
Description

The galaxy war between the Empire Draco and the Commonwealth of Zibu broke out 3 years ago. Draco established a line of defense called Grot. Grot is a straight line with N defense stations. Because of the cooperation of the stations, Zibu’s Marine Glory cannot march any further but stay outside the line.

A mystery Information Group X benefits form selling information to both sides of the war. Today you the administrator of Zibu’s Intelligence Department got a piece of information about Grot’s defense stations’ arrangement from Information Group X. Your task is to determine whether the information is reliable.

The information consists of M tips. Each tip is either precise or vague.

Precise tip is in the form of P A B X, means defense station A is X light-years north of defense station B.

Vague tip is in the form of V A B, means defense station A is in the north of defense station B, at least 1 light-year, but the precise distance is unknown.

Input

There are several test cases in the input. Each test case starts with two integers N (0 < N ≤ 1000) and M (1 ≤ M ≤ 100000).The next M line each describe a tip, either in precise form or vague form.

Output

Output one line for each test case in the input. Output “Reliable” if It is possible to arrange N defense stations satisfying all the M tips, otherwise output “Unreliable”.

Sample Input

3 4
P 1 2 1
P 2 3 1
V 1 3
P 1 3 1
5 5
V 1 2
V 2 3
V 3 4
V 4 5
V 3 5
Sample Output

Unreliable
Reliable

题意

给出两个指令,P A B X ,表示a在b的前面x米。
V A B ,A在B前面不知道多少米。问这些信息有没有矛盾

题解


d[a]-d[b]=X
这个结果可以用两个不等式表示
d[a]-d[b]>=x&&d[a]-d[b]<=x
a至少在b前1米
即d[a]>=d[b]+1
按这些约束条件进行Bllman-ford
*用c++提交AC,用G++提交RE

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <string>
#include <set>
#include <cmath>
#include <map>
#include <queue>
#include <sstream>
#include <vector>
#include <iomanip>
#define m0(a) memset(a,0,sizeof(a))
#define mm(a) memset(a,0x3f,sizeof(a))
#define m_1(a) memset(a,-1,sizeof(a))
#define f(i,a,b) for(i = a;i<=b;i++)
#define fi(i,a,b) for(i = a;i>=b;i--)
#define lowbit(a) ((a)&(-a))
#define FFR freopen("data.in","r",stdin)
#define FFW freopen("data.out","w",stdout)
#define INF 0x3f3f3f3f
typedef long long ll;
typedef long double ld;
const ld PI = acos(-1.0);

using namespace std;
#define SIZE ( )

struct Edge {
    int u, v, k;
};

struct Edge1 {
    Edge edge[100000 + 10];
    int cnt;
    void push_back(int u, int v, int k) {
        edge[cnt].u = u;
        edge[cnt].v = v;
        edge[cnt].k = k;
        cnt++;
    }
};

Edge1 G;
int d[1234];

int main() {
    //ios_base::sync_with_stdio(false); cin.tie(0);
    int n, m;
    while (~scanf("%d%d",&n,&m)) {
        char s[5];
        int i,j;
        G.cnt = 0;
        f(i, 1, m) {
            scanf("%s", s);
            if ('P' == s[0]) {
                int u, v, k;
                scanf("%d%d%d", &u, &v, &k);
                G.push_back(u, v, -k);
                G.push_back(v, u, k);
            } else {
                int u, v;
                scanf("%d%d", &u, &v);
                G.push_back(u, v, -1);
            }
        }
        m0(d);
        int ok = 0;
        f(i, 1, n) {
            ok = 0;
            f(j, 0, G.cnt - 1) {
                if(d[G.edge[j].v] > d[G.edge[j].u] + G.edge[j].k){
                    d[G.edge[j].v] = d[G.edge[j].u] + G.edge[j].k;
                    ok = 1;
                }

            }
            if(!ok){
                ok = 1;
                break;
            }
        }

        if (i<=n) printf("Reliable\n");
        else printf("Unreliable\n");
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值