poj2983——Is the Information Reliable?(差分約束+負權環路)

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,和模糊情報v,p給出了兩個點a,b之間的準確距離c,v只給出a到b的距離至少爲1,並且所有點在一條直線上。求這些情報是否可靠。

對於p情報,不僅能獲得正向的邊權值,還能算出反向的負權,如果後面給出的情報不對,就會形成一條負權的環路,把第一個樣例畫一畫就知道了。用spfa求是否有負權環路即可

#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <math.h>
#include <algorithm>
#include <queue>
#include <iomanip>
#include <map>
#include <cctype>
#define INF 0x3f3f3f3f
#define MAXN 1000005
#define Mod 1000000007
using namespace std;
struct Edge
{
    int v,w,next;
};
Edge edge1[MAXN<<1];
int head1[MAXN],n,m,e,vis[MAXN],dis[MAXN];
int p[MAXN];
void add(Edge *edge,int *head,int u,int v,int w)
{
    edge[e].v=v;
    edge[e].w=w;
    edge[e].next=head[u];
    head[u]=e;
    e++;
}
bool spfa(Edge *edge,int *head,int u)
{
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;++i)
        dis[i]=-INF;
    dis[u]=0;
    queue<int> q;
    q.push(u);
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        vis[u]=0;
        p[u]++;
        if(p[u]>n)
            return false;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v,w=edge[i].w;
            if(w+dis[u]>dis[v])
            {
                dis[v]=w+dis[u];
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    return true;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        e=0;
        memset(head1,-1,sizeof(head1));
        memset(p,0,sizeof(p));
        for(int i=0;i<m;++i)
        {
            int a,b,c;
            char op;
            cin>>op;
            if(op=='P')
            {
                scanf("%d%d%d",&a,&b,&c);
                add(edge1,head1,a,b,c);
                add(edge1,head1,b,a,-c);
            }
            else
            {
                scanf("%d%d",&a,&b);
                add(edge1,head1,a,b,1);
            }
        }
        for(int i=1;i<=n;++i)
            add(edge1,head1,0,i,0);
        if(spfa(edge1,head1,0))
            printf("Reliable\n");
        else
            printf("Unreliable\n");
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章