Time Limit: 3000MS | Memory Limit: 131072K | |
Total Submissions: 13180 | Accepted: 4164 |
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.
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
给一段信息,有关于N个点,M条信息。其中每一条信息:
1、P A B X,意思是A站点在B站点北X光年
2、V A B,意思是A站点在B站点北,但不知道确切距离,最少一光年。
对于给出的这段信息,求是否是真是的消息,因为错误的消息有矛盾的信息。
用SPFA:
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <queue>
#define M 100002
using namespace std;
struct node
{
int to, next;
int dist;
}E[3*M];
int top, n, m;
bool vis[M];
int dis[M], head[M];
int sum[M];
void inti()
{
int i;
top = 0;
for ( i = 1;i <= n; i++ )
{
head[i] = -1;
dis[i] = 0;
sum[i] = 0;
}
}
void AddEdge(int u, int v, int dist)
{
E[top].to = v;
E[top].dist = dist;
E[top].next = head[u];
head[u] = top++;
}
bool SPFA()
{
int i;
queue<int> q;
for ( i = 1;i <= n; i++ )
{
q.push(i);
vis[i] = true;
}
while ( !q.empty() )
{
int u = q.front();
q.pop();
vis[u] = false;
for ( i = head[u];i != -1;i = E[i].next )
{
int v = E[i].to;
if ( dis[v] > dis[u]+E[i].dist )
{
dis[v] = dis[u]+E[i].dist;
if ( !vis[v] )
{
vis[v] = true;
q.push(v);
sum[v]++;
if ( sum[v] >= n )
return false; }
}
}
}
return true;
}
int main()
{
char ch[5];
int i;
int u, v, dist;
while ( ~scanf ( "%d %d", &n, &m ) )
{
inti();
for ( i = 0;i < m; i++ )
{
scanf ( "%s", ch );
if ( ch[0] == 'P' )
{
scanf ( "%d %d %d", &u, &v, &dist );
AddEdge(v, u, dist);
AddEdge(u, v, -dist);
}
else
{
scanf ( "%d %d", &u, &v );
AddEdge(u, v, -1);
}
}
printf ( SPFA() ? "Reliable\n" : "Unreliable\n" );
}
return 0;
}
用Bellman-Frod
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
#define M 100002
using namespace std;
struct node
{
int u, v;
int dist;
}E[3*M];
int top;
int dis[M];
void AddEdge(int u, int v, int dist)
{
E[top].u = u;
E[top].v = v;
E[top].dist = dist;
top++;
}
int main()
{
char ch[5];
int n, m, j, i;
int u, v, dist;
while ( ~scanf ( "%d %d", &n, &m ) )
{
top = 0;
for ( i = 0;i < m; i++ )
{
scanf ( "%s", ch );
if ( ch[0] == 'P' )
{
scanf ( "%d %d %d", &u, &v, &dist );
AddEdge(v, u, dist);
AddEdge(u, v, -dist);
}
else
{
scanf ( "%d %d", &u, &v );
AddEdge(u, v, -1);
}
}
memset ( dis, 0, sizeof(dis) );
for ( i = 1;i < n; i++ )
{
for ( j = 0;j < top; j++ )
{
if ( dis[E[j].v] > dis[E[j].u]+E[j].dist )
dis[E[j].v] = dis[E[j].u]+E[j].dist;
}
}
for ( j = 0;j < top; j++ )
{
if ( dis[E[j].v] > dis[E[j].u]+E[j].dist )
break;
}
if ( j >= top )
printf ( "Reliable\n" );
else
printf ( "Unreliable\n" );
}
return 0;
}
代码菜鸟,如有错误,请多包涵!!!
如有帮助记得支持我一下,谢谢!!!