POJ 2983 (差分约束系统Spfa)

题意:有一些关于点的信息,大概如下。P A B C 表示点A在点B的北方且与B相距C。 V A B 表示点A在B的北方,但是不清楚具体的距离(A-B>=1)。
题解:A-B=C转换成A-B>=C和A-B<=C就可以了。

#include <iostream>
using namespace std;

#define N  200005
#define INF 99999999

int n, m, size;
bool mark[N];
int head[N], dis[N];
int que[N], inque[N];

struct Edge { int v, w, next; };
Edge  edge[N];

bool Spfa()
{
	memset(mark,0,sizeof(mark));
	memset(inque,0,sizeof(inque));
	for ( int i = 0; i <= n; i++ )
		dis[i] = INF;

	int u, v, front, rear;
	front = rear = 0;
	mark[0] = true;
	inque[0]++;
	dis[0] = 0;
	que[rear] = 0;
	rear = ( rear + 1 ) % N;

	while ( front != rear )
	{
		u = que[front];
		front = ( front + 1 ) % N;
		mark[u] = false;
		for ( int i = head[u]; i; i = edge[i].next )
		{
			v = edge[i].v;
			if ( dis[v] > dis[u] + edge[i].w )
			{
				dis[v] = dis[u] + edge[i].w;
				if ( ! mark[v] )
				{
					que[rear] = v;
					rear = ( rear + 1 ) % N;
					mark[v] = true;
					inque[v]++;
					if ( inque[v] >= n+1 ) return false;
					
				}
			}
		}
	}
	return true;
}

void add ( int u, int v, int w )
{
	size++;
	edge[size].v = v;
	edge[size].w = w;
	edge[size].next = head[u];
	head[u] = size;
}

int main()
{
	char ch;
	int a, b, c;
	while ( scanf("%d%d",&n, &m) != EOF )
	{
		size = 0;
		memset(head,0,sizeof(head));

		while ( m-- )
		{
			getchar();
			scanf("%c",&ch);
			if ( ch == '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() )
			printf("Reliable\n");
		else
		    printf("Unreliable\n");	
	}
	return 0;
}

		
		


你可能感兴趣的:(c,struct)