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 Vague tip is in the form of 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
POJ Monthly--2006.08.27, Dagger
|
提示
题意:
某集团建立了n个防御站,在战争中获取了m(1<=m<=100000)个情报,情报是这样给出的:
P A B X表示A站到B站相隔X光年的距离。
V A B表示A站到B站相隔至少1光年的距离,具体不清楚。
请你看看这份情报是否可靠。
思路:
不等式组:
P A B X
1.B-A>=X
2.A-B<=-X
V A B
3.A-B<=-1
接下来就是构建边,数据会有孤立点的情况,要设置一个假想源点,这样就只是求最短路有没有负环,也就是该点是否入队超过n次,有表示不可靠,没有则可靠。
我用邻接矩阵死活都是哇,改成邻接表才过。(ˇˍˇ) 想~
示例程序
Source Code
Problem: 2983 Code Length: 1773B
Memory: 2920K Time: 516MS(485MS) //括号里的为数组模拟,队列似乎不用开很大
Language: G++ Result: Accepted
#include <cstdio>
#include <cstring>
#include <queue>
#define MAX 1000000007
using namespace std;
struct edge
{
int v,d,next;
}w[201000];
int h[1001],numw;
void insert(int u,int v,int c)
{
w[numw].v=v;
w[numw].d=c;
w[numw].next=h[u];
h[u]=numw;
numw++;
}
int spfa(int n)
{
int d[1001],v[1001],num[1001],i,pos,pos1;
for(i=0;n>=i;i++)
{
d[i]=MAX;
v[i]=0;
num[i]=0;
}
queue<int>q;
d[0]=0; //从假想源点开始
num[0]++;
q.push(0);
v[0]=1;
while(q.empty()==0)
{
pos=q.front();
q.pop();
v[pos]=0;
for(i=h[pos];i!=-1;i=w[i].next)
{
pos1=w[i].v;
if(d[pos1]>d[pos]+w[i].d)
{
d[pos1]=d[pos]+w[i].d;
if(v[pos1]==0)
{
q.push(pos1);
num[pos1]++;
if(num[pos1]>n) //存在负环
{
return 0;
}
v[pos1]=1;
}
}
}
}
return 1;
}
int main()
{
int i,n,m,u,v,w;
char c[2];
while(scanf("%d %d",&n,&m)!=EOF)
{
numw=0;
memset(h,-1,sizeof(h));
for(i=1;m>=i;i++)
{
scanf("%s %d %d",c,&u,&v);
if(c[0]=='P')
{
scanf("%d",&w);
insert(v,u,w); //各种加边
insert(u,v,-w);
}
else
{
insert(u,v,-1);
}
}
for(i=1;n>=i;i++) //假想源点
{
insert(0,i,0);
}
if(spfa(n)==0)
{
printf("Unreliable\n");
}
else
{
printf("Reliable\n");
}
}
return 0;
}