| Time Limit: 3000MS | Memory Limit: 131072K | |
| Total Submissions: 12669 | Accepted: 3984 |
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 withN 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 stationB, 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 integersN (0 <N ≤ 1000) and M (1 ≤ M ≤ 100000).The nextM 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 arrangeN defense stations satisfying all theM 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 a-b>=c (正向 ) 和 b-a>=-c (反向)
v a-b>=1;
如上图:1~2 和 2~1 这个环路权值为 0 ,同理 1 ~3这个环路
但是当 1 ~3 的权值小于二的时候或者小于 一的时候就会出现负权回路
出现了负权回路就是 unreliable
因为spfa不能跑不连通的图,0号点(超级源)的初始d设为0.然后求从0号点到其他所有点的最短距离
#include<queue>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f
int n,m;
int num[100010],vis[100010];
int head[400010],dis[100010];
struct node
{
int v,w,next;
}path[400010];
int top;
void add(int u,int v,int w)
{
path[top].v=v;
path[top].w=w;
path[top].next=head[u];
head[u]=top++;
}
bool SPFA()
{
memset(vis,0,sizeof(vis));
memset(num,0,sizeof(num));
memset(dis,INF,sizeof(dis));
queue<int>q;
dis[0]=0;
q.push(0);
while(q.size())
{
int p=q.front();
q.pop();
vis[p]=0;
for(int i=head[p];i!=-1;i=path[i].next){
int t=path[i].v;
if(dis[t]>dis[p]+path[i].w){
dis[t]=dis[p]+path[i].w;
if(vis[t]==0){
q.push(t);
num[t]++;
vis[t]=1;
if(num[t]>n)
return false;
}
}
}
}
return true;
}
int main()
{
char ch[5];
int a,b,c;
//freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&m)==2)
{
top=0;
memset(head,-1,sizeof(head));
while(m--)
{
scanf("%s",ch);
if(ch[0]=='P'){
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,-c);
}
else{
scanf("%d%d",&a,&b);
add(a,b,1);
}
}
for(int i=1;i<=n;i++)
add(0,i,0);
if(SPFA())
puts("Reliable");
else
puts("Unreliable");
}
return 0;
}

本文介绍了一种通过构建图模型并运用SPFA算法来判断战争中接收到的信息是否可靠的方法。针对不同形式的情报提示,文章详细阐述了如何建立边权及如何避免出现负权回路,最终确定信息的有效性。
1037

被折叠的 条评论
为什么被折叠?



