ACM: 差分约束 图论题 poj 2983 sp…

来源:互联网 发布:麻瓜编程 爬虫 编辑:程序博客网 时间:2024/05/18 11:28
                                                      Is the Information Reliable?

Description

The galaxy war between theEmpire Draco and the Commonwealth of Zibu broke out 3 years ago.Draco established a line of defense called Grot. Grot is a straightline with N defense stations. Because of the cooperation ofthe stations, Zibu’s Marine Glory cannot march any further but stayoutside the line.

A mystery Information Group X benefits form selling informationto both sides of the war. Today you the administrator of Zibu’sIntelligence Department got a piece of information about Grot’sdefense stations’ arrangement from Information Group X. Your taskis to determine whether the information is reliable.

The information consists of M tips. Each tip is eitherprecise or vague.

Precise tip is in the form of P A B X, meansdefense station A is X light-years north of defensestation B.

Vague tip is in the form of V A B, means defensestation A is in the north of defense station B, atleast 1 light-year, but the precise distance is unknown.

Input

There are several test casesin 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 inprecise form or vague form.

Output

Output one line for eachtest case in the input. Output “Reliable” if It is possible toarrange 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代表确却的位置.

      即: 'P' f[u] -f[v] >=   'V'   f[u] - f[v]>= 1

 

解题思路:

         1. 差分约束系统的题. 一开始spfa + 邻接表. 10多次的TLE阿~

          2.图论题还是建图为重点. 邻接表.

           正向时:

                   f[u] - f[v] >= x

           反向时:

                   f[v] - f[u] >= -x

          3.一开始用spfa都TLE疯掉了. 还是bellman_ford确定O(n*m)+判断负环

 

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
#define MAX 300005
const int INF = (1<<30);

struct node
{
 int u, v;
 int w;
 int next;
};

int n, m;
int first[MAX];
node edges[MAX];
int num;

void read_graph()
{
 num = 0;
 memset(edges,0,sizeof(edges));
 memset(first,-1,sizeof(first));
 int i;
 char ch;
 int u, v, w;
 for(i = 0; i < m; ++i)
 {
  getchar();
  scanf("%c",&ch);
  if(ch == 'V')
  {
   scanf("%d%d",&u,&v);
   edges[num].u= u;
   edges[num].v= v;
   edges[num].w= -1;
   edges[num].next= first[u];
   first[u] =num;
   num++;
  }
  else if(ch == 'P')
  {
   scanf("%d %d%d",&u,&v,&w);
   edges[num].u= u;
   edges[num].v= v;
   edges[num].w= -w;
   edges[num].next= first[u];
   first[u] =num;
   num++;

   edges[num].u= v;
   edges[num].v= u;
   edges[num].w= w;
   edges[num].next= first[v];
   first[v] =num;
   num++;
  }
 }
}

bool bellman_ford()
{
 int dist[MAX];
 for(int i = 0; i < n+1; ++i)
 {
  dist[i] = (i == 0 ? 0 :INF);
 }

 for(int k = 1; k < n;++k)
 {
  int t = 0; //标记. 关键否则TLE
  for(int j = 0; j< num; ++j)
  {
   if(dist[edges[j].v]> dist[edges[j].u] + edges[j].w) //松弛操作
   {
    dist[edges[j].v]= dist[edges[j].u] + edges[j].w;
    t= 1;
   }
  }
  if(t == 0)//如果没有进行松弛操作表示已经是最短路
   break;
 }

 bool flag = true;
 for(int e = 0; e < num;++e)  //判断负环
 {
  if(dist[edges[e].v]> dist[edges[e].u] + edges[e].w)
  {
   flag =false;
   break;
  }
 }
 if(flag)
  return false;
 else
  return true;
}

int main()
{
 freopen("input.txt","r",stdin);
 while(scanf("%d%d",&n,&m) != EOF)
 {
  read_graph();
  if(bellman_ford())
   printf("Unreliable\n");
  else
   printf("Reliable\n");
 }
 return 0;
}

0 0
原创粉丝点击