题目大意:
给出n个防御站,m个不等式,判断是否有可行解
思路:
这是一道关于查分约束的题,解题的关键就是找到不等关系,构成不等式组,然后用SPFA判断是否具有负环
P A B X : Dis[a] = Dis[b] + X;
V A B : Dis[a] = Dis[b] + 1;
即:
P: Dis[a] - DIs[b] <= X
Dis[b] - Dis[a] <= -X
V: Dis[a] - Dis[b] <=1
由于是用的SPFA算法,建立的图中的每一个点不一定是联通的,所以需要建立一个虚点将所有的点连接
/* ***********************************************
Author :sunming
Created Time :2016年02月18日 星期四 15时46分46秒
File Name :a.cpp
************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
//#include <time.h>
//#include <list.h>
//#include <vector.h>
using namespace std;
#pragma comment(linker, "/STACK:1024000000")
#define LL long long
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
const double eqs=1e-9;
const int MAXN=100100+10;
struct node
{
int v,w,next;
} Edge[MAXN*3+10];
int Dis[1010],head[1010],cnt[1010];
bool vis[1010];
int top;
void Add(int u, int v, int w)
{
Edge[top].v = v;
Edge[top].w = w;
Edge[top].next = head[u];
head[u] = top++;
}
bool SPFA(int n)
{
queue<int >Q;
for(int i=0; i<=n; i++)
{
Dis[i] = INF;
cnt[i] = 0;
vis[i] = false;
}
Dis[0] = 0;
Q.push(0);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
if(++cnt[u] > n)
return false;
vis[u] = false;
for(int i=head[u]; i!=-1; i=Edge[i].next)
{
int v = Edge[i].v;
int w = Edge[i].w;
if(Dis[v] > Dis[u] + w)
{
Dis[v] = Dis[u] + w;
if(!vis[v])
{
vis[v] = true;
Q.push(v);
}
}
}
}
return true;
}
int main()
{
int n,m,u,v,w;
while(~scanf("%d%d",&n,&m))
{
memset(head, -1, sizeof(head));
top = 0;
while(m--)
{
char str[2];
scanf("%s%d%d",str,&u,&v);
if(!strcmp(str,"P"))
{
scanf("%d",&w);
Add(u,v,w);
Add(v,u,-w);
}
else
Add(v,u,-1);
}
for(int i=1; i<=n; i++)
Add(0,i,0);
if(SPFA(n))
printf("Reliable\n");
else
printf("Unreliable\n");
}
return 0;
}