现在的位置: 首页 > 算法 > 正文

poj 2983 差分约束+SPFA

2019年02月28日 算法 ⁄ 共 2938字 ⁄ 字号 评论关闭
Is the Information Reliable?
Time Limit: 3000MS   Memory Limit: 131072K
Total Submissions: 9890   Accepted: 3067

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

题意:第一行n,m,代表有n个点和m条情报,接下来m行,以P开头的后跟3个数字a、b、c,代表a在b的北方距离c处,以V开头的后跟俩数字a、b,代表a在b北方至少距离为1的地方,让你判断情报是否可信。
思路:由此可以建立不等式与方程,其中方程转化为等价的两个不等式,如第一组样例,可以变为不等式组:d2-d1<=-1,d1-d2<=1,d3-d2<=-1,d2-d3<=1,d3-d1<=-1,d1-d3<=1(重复的已去除)。这样问题就变成了不等式组有没有解的问题,有解则情报可信,没解则情报不可信,好吧,这里就是一个差分约束问题,什么?你不知道差分约束?那你自己百度一下吧。
下面是用SPFA写的程序,题目有坑,对于给你俩点编号相同的情况要特殊处理。
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
struct node
{
    int a,b,c;
    int next;
}e[202005];
int fst[1005];
bool v[1005];
int m,n,a,b,c,num;
int cnt[1005];
char p[5];
int dist[1005];
bool spfa()//spfa判断是否有解
{
    for(int i=0;i<=n;i++)dist[i]=1;//比0点到它的边的长度大即可
    dist[0]=0;
    queue<int>q;
    q.push(0);
    cnt[0]++;
    v[0]=1;
    while(!q.empty())
    {
        a=q.front();
        q.pop();
        for(int i=fst[a];i!=-1;i=e[i].next)
        {
            b=e[i].b;
            c=e[i].c;
            if(dist[b]>dist[a]+c)
            {
                dist[b]=dist[a]+c;
                if(!v[b])
                {
                    q.push(b);
                    v[b]=1;
                    cnt[b]++;
                    if(cnt[b]>n)return false;//说明有负环路,无解退出
                }
            }
        }
        v[a]=0;
    }
    return true;
}
int main()
{
    int ans;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        num=0;
        ans=0;
        memset(fst,-1,sizeof(fst));
        memset(cnt,0,sizeof(cnt));
        memset(v,0,sizeof(v));
        for(int i=0;i<m;i++)
        {
            scanf("%s",&p);
            if(p[0]=='P')
            {
                scanf("%d%d%d",&a,&b,&c);
                if(a==b&&c)ans=1;//自己连自己的点特殊处理
                                e[num].next=fst[a];//等式变为俩不等式,即建立双向边,这里邻接表存储
                fst[a]=num;
                e[num].a=a;
                e[num].b=b;
                e[num++].c=-c;
                e[num].next=fst[b];
                fst[b]=num;
                e[num].a=b;
                e[num].b=a;
                e[num++].c=c;
            }
            else
            {
                scanf("%d%d",&a,&b);
                if(a==b)ans=1;//自己连自己的特殊处理
                e[num].next=fst[a];//不等式只建立单向边
                fst[a]=num;
                e[num].a=a;
                e[num].b=b;
                e[num++].c=-1;
            }
        }
        for(int i=1;i<=n;i++)
        {
            e[num].next=fst[0];
            fst[0]=num;
            e[num].a=0;
            e[num].b=i;
            e[num++].c=0;
        }
        if(ans)cout<<"Unreliable"<<endl;
        else if(spfa())cout<<"Reliable"<<endl;
        else cout<<"Unreliable"<<endl;
    }
    return 0;
}
【上篇】
【下篇】

抱歉!评论已关闭.