(差分约束系统) poj 2983

Is the Information Reliable?

Time Limit: 3000MS   Memory Limit: 131072K
Total Submissions: 11676   Accepted: 3687

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

Source

POJ Monthly--2006.08.27, Dagger

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#define INF 100000000
using namespace std;
int n,m,cnt,in[1010],dist[1010];
bool vis[1010];
char c;
struct node
{
    int to,len,next;
}e[210000];
int head[1010];
void add(int u,int v,int len)
{
    e[++cnt].to=v;
    e[cnt].len=len;
    e[cnt].next=head[u];
    head[u]=cnt;
}
bool spfa(int u)
{
    memset(in,0,sizeof(in));
    memset(vis,0,sizeof(vis));
    for(int i=0;i<=n;i++)
        dist[i]=INF;
    vis[u]=1;
    dist[u]=0;
    queue<int> q;
    q.push(u);
    in[u]++;
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        vis[x]=0;
        for(int i=head[x];i;i=e[i].next)
        {
            int v=e[i].to;
            int w=e[i].len;
            if(dist[v]>dist[x]+w)
            {
                dist[v]=dist[x]+w;
                if(!vis[v])
                {
                    q.push(v);
                    vis[v]=1;
                    in[v]++;
                    if(in[v]>n+1)
                        return false;
                }
            }
        }
    }
    return true;
}
int main()
{
    int a,b,w;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        cnt=1;
        memset(head,0,sizeof(head));
        for(int i=1;i<=m;i++)
        {
            cin>>c;
            if(c==‘P‘)
            {
                scanf("%d%d%d",&a,&b,&w);
                add(b,a,-w);
                add(a,b,w);
            }
            else if(c==‘V‘)
            {
                scanf("%d%d",&a,&b);
                add(b,a,-1);
            }
        }
        for(int i=1;i<=n;i++)
            add(0,i,0);
        if(spfa(0))
            printf("Reliable\n");
        else
            printf("Unreliable\n");
    }
    return 0;
}

  

时间: 2024-08-04 01:17:08

(差分约束系统) poj 2983的相关文章

(差分约束系统) poj 1201

Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22781   Accepted: 8613 Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a program that: reads the number of intervals, their end po

POJ 2983 Is the Information Reliable?(差分约束系统)

题目地址:POJ 2983 这题刚上来完全不知道跟差分约束系统有什么关系.....后来发现只要判个负环就可以.. 因为假如有冲突的话会形成一个负环.之所以建图加上一个正值一个负值,是因为这样的话,像1 2 4和1 2 3这样的数据就会形成一个负环.这个方法还是很巧妙的...然后对于V的那些不清楚的位置,就会跟P的那些等式联立形成一个不等式,然后在用最短路判环的过程中就用松弛来解决. 代码如下: #include <iostream> #include <cstdio> #inclu

【POJ 2983】Is the Information Reliable?(差分约束系统)

[POJ 2983]Is the Information Reliable?(差分约束系统) Is the Information Reliable? Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 12244   Accepted: 3861 Description The galaxy war between the Empire Draco and the Commonwealth of Zibu broke ou

【poj 2983】Is the Information Reliable? 差分约束

题目:http://poj.org/problem?id=2983 Is the Information Reliable? Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 12535 Accepted: 3943 Description The galaxy war between the Empire Draco and the Commonwealth of Zibu broke out 3 years ago. Dr

【POJ 1201】 Intervals(差分约束系统)

[POJ 1201] Intervals(差分约束系统) 11 1716的升级版 把原本固定的边权改为不固定. Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23817   Accepted: 9023 Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a p

POJ 3169 Layout(差分约束系统)

题目链接:http://poj.org/problem?id=3169 题意:n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0.这些牛的距离存在着一些约束关系:1.有ML组(u, v, w)的约束关系,表示牛[u]和牛[v]之间的距离必须 <= w.2.有MD组(u, v, w)的约束关系,表示牛[u]和牛[v]之间的距离必须 >= w.问如果这n头无法排成队伍,则输出-1,如果牛[1]和牛[n]的距离可以无限远,则输出-2,否则则输出牛[1]和牛[n]之间的最

POJ 2983 Is the Information Reliable? 差分约束

裸差分约束. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<iostream> #include<sstream> #include<cmath> #include<

【POJ 1716】Integer Intervals(差分约束系统)

[POJ 1716]Integer Intervals(差分约束系统) Integer Intervals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13425   Accepted: 5703 Description An integer interval [a,b], a < b, is a set of all consecutive integers beginning with a and ending w

POJ 2983-Is the Information Reliable?(差分约束系统)

题目地址:POJ 2983 题意:有N个车站.给出一些点的精确信息和模糊信息.精确信息给出两点的位置和距离.模糊信息给出两点的位置.但距离大于等于一.试确定是否全部的信息满足条件. 思路:事实上就是让你推断是否存在负环.好久才看明确.对于精确消息.能够得出两个差分公式:dis[v] <= dist[u] - w  &&  dist[u] <= dist[v] + w.对于模糊信息.能够得出dis[v] <= dis[u] -1. PS:做差分约束感觉还是Bellman_f