poj2983——差分约束,bellman_ford

poj2983——差分约束,bellman_ford

Is the Information Reliable?

Time Limit: 3000MS   Memory Limit: 131072K
Total Submissions: 11560   Accepted: 3658

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题意:如题思路;用spfa须增设超级源点,所以又多了N条边,故TLE,而bellman_ford则可以直接处理

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>

using namespace std;

const int maxn=10010;
const int INF=(1<<28);

int N,M;
int a,b,x;
struct Edge
{
    int u,v,w;
};Edge edge[maxn*100];int e;
char ch;
int dist[maxn];

void add_edge(int u,int v,int w)
{
    edge[e++]={u,v,w};
}

bool relax(int cur)
{
    int tmp=dist[edge[cur].u]+edge[cur].w;
    if(tmp>dist[edge[cur].v]){
        dist[edge[cur].v]=tmp;
        return true;
    }
    return false;
}

bool bellman_ford()
{
    memset(dist,0,sizeof(dist));
    for(int i=0;i<N;i++){
        bool flag=0;
        for(int j=0;j<e;j++){
            if(relax(j)) flag=1;
        }
        if(!flag) return true;
    }
    for(int i=0;i<e;i++){
        if(relax(i)) return false;
    }
    return true;
}

int main()
{
    while(cin>>N>>M){
        e=0;
        char ch;
        while(M--){
            getchar();
            scanf("%c",&ch);
            if(ch==‘P‘){
                //cin>>a>>b>>x;
                scanf("%d%d%d",&a,&b,&x);
                add_edge(b,a,x);
                add_edge(a,b,-x);
            }
            else{
                scanf("%d%d",&a,&b);
                add_edge(b,a,1);
            }
        }
        if(bellman_ford()) cout<<"Reliable"<<endl;
        else cout<<"Unreliable"<<endl;
    }
    return 0;
}

时间: 2024-10-12 22:50:18

poj2983——差分约束,bellman_ford的相关文章

【POJ2983】Is the Information Reliable? ——差分约束

题目大意:一天南北线上有n个防御站,给出他们之间的位置关系,问有没有可能存在这样一种位置布置符合所给的位置关系.关系有两种,一种是 P A B X,表示A在B北边X光年的位置,V A B表示A在B北边至少1光年位置. 分析:仍然考虑差分约束,容易想到,若关系为P,则 s[a]-a[b]=c; 变换一下则变成: s[b]-s[a]<=-c; s[a]-a[b]<=c; 若关系为V,则 s[b]-s[a]<=-1; 这样转换之后就变成了求最短路了,注意这道题可能无解,无解即表现为存在负环,判

POJ 1364 King --差分约束第一题

题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析:典型差分约束题,变换,令Ti = SUM(Xj) (0<=j<=i).  则表达式(1)可以看做T(a+b)-T(a-1) > k,也就是T(a-1)-T(a+b) < -k,又因为全是整数,所以T(a-1)-T(a+b) <= -k-1.  同理,(2)看做T(a+b)-T(

poj 1364 King(差分约束)(中等)

King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11028   Accepted: 4040 Description Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen prayed: ``If my child was a son and if only he was a sound kin

shortpath1364差分约束

差分约束 题目大意:现在假设有一个这样的序列,S={a1,a2,a3,a4...ai...at} 其中ai=a*si,其实这句可以忽略不看 现在给出一个不等式,使得ai+a(i+1)+a(i+2)+...+a(i+n)<ki或者是ai+a(i+1)+a(i+2)+...+a(i+n)>ki 首先给出两个数分别代表S序列有多少个,有多少个不等式 不等式可以这样描述 给出四个参数第一个数i可以代表序列的第几项,然后给出n,这样前面两个数就可以描述为ai+a(i+1)+...a(i+n),即从i到n

poj 3169 差分约束

3169 差分约束的是满足多组形如xi-yj<=bk{i,j<n k<m}不等式极值问题,可以转化为单源最短路来求. 在最短路中 d[v]<=d[u]+w(u,v) 可以看出跟上面的不等式很像 通常有两种一种是求满足所有不等式的最大值,还有是最小值. 这篇博客可以参考一下 分析: 题目给出了两种不等式  d[u]+dl >=d[v]       d[u]+dd<=d[v]  要求的是最大值,也就是最短路 对于d[u]+dl>=d[v] 建边(u,vdl) 对于d[

差分约束

1.bzoj3436 思路: 差分约束根据限制条件建图,注意要有一个超级源点向所有点连一条边权为0的边建图看代码. 然后spfa判负环,写bfs会超时的......实测n遍. #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #define inf 0x7fffffff #define ll long long #define N 100007 using na

bzoj2788 festival 差分约束

填坑中--链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2788 题意: 有$n$个正整数$X1,X2,...,Xn$,再给出$m1+m2$个限制条件,限制分为两类:1. 给出$a,b(1<=a,b<=n)$,要求满足$Xa + 1 = Xb$2. 给出$c,d (1<=c,d<=n)$,要求满足$Xc <= Xd$在满足所有限制的条件下,求集合${Xi}$大小的最大值. 首先看情况我们也知道是差分约束-- 但是这个差分

POJ 1201 Intervals 差分约束

http://poj.org/problem?id=1201 TLE了很久,因为用了cin..... 思路和其他差分约束差不多,http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html 如果区间[a, b]中至少有c个元素,如果用上面的博客,那么说明xa - xb >= c,但是注意这里是闭区间,xa - xb是不包括b这个点的, 就比如用了[a, b]有c个元素,[b, d]有x个,那么ans = c + x - 1个,

【bzoj2330】: [SCOI2011]糖果 图论-差分约束-SPFA

[bzoj2330]: [SCOI2011]糖果 恩..就是裸的差分约束.. x=1 -> (A,B,0) (B,A,0) x=2 -> (A,B,1)  [这个情况加个A==B无解的要特判] x=3 -> (B,A,0)  [恩这个是不少于一开始zz建反了] x=4 -> (B,A,1) x=5 -> (A,B,0) 然后源点到所有点建1的边[恩据说有条链所以要反着连]跑最长路就好了 1 /* http://www.cnblogs.com/karl07/ */ 2 #inc