POJ 2983 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.

solution

正解:差分约束
对于相等,我们就将大于等于和小于等于的限制都加上,对于大于我们还是反向连负边,最后判断是否有负环即可,可以考虑用dfs版

    #include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
const int N=1005,M=200000;
int n,m,nxt[M],to[M],dis[M],num=0,head[N];char s[2];
il void link(int x,int y,int z){
   nxt[++num]=head[x];to[num]=y;dis[num]=z;head[x]=num;}
bool flag=1,vis[N];int f[N];
void Clear(){
   for(RG int i=0;i<N;i++)head[i]=f[i]=vis[i]=0;
   flag=0;num=0;
}
il bool dfs(int x){
   vis[x]=1;
   for(RG int i=head[x];i;i=nxt[i]){
      int u=to[i];
      if(f[x]+dis[i]<f[u]){
         if(vis[u])return false;
         f[u]=f[x]+dis[i];
         if(!dfs(u))return false;
      }
   }
   vis[x]=0;
   return true;
}
void work()
{
   Clear();
   int x,y,z;
   for(int i=1;i<=m;i++){
      scanf("%s%d%d",s,&x,&y);
      if(s[0]=='V')
         link(y,x,-1);
      else{
         scanf("%d",&z);
         link(x,y,z);link(y,x,-z);
      }
   }
   for(int i=1;i<=n;i++){
      if(!dfs(i)){
         puts("Unreliable");
         return ;
      }
   }
   puts("Reliable");
}

int main(){
	while(~scanf("%d%d",&n,&m))work();
	return 0;
}

posted @ 2017-10-29 14:46  Hxymmm  阅读(193)  评论(0编辑  收藏  举报