POJ 2983 Is the Information Reliable? (Differential restraint system)

Topic Link

The meaning of problems

A PABX represents the exact distance north point B is X

A VAB represents the north side B, the specific distance uncertain, but the distance must be greater than 1.

He asked whether there is a case of the N condition before stronghold meet.

Thinking

Determining a distance between two points x, corresponding to

a - b >= x
&&
a - b <= x

Another condition is that

a - b >= 1

By these two sides built information, can find the shortest path may be the longest, the difference is, the shortest loop negative determination, the longest road judging ring n

PS:

Hang death of me, while at most 20w + 1000 Ge, I opened an array of small, I opened 11w, it prompts me TLE, served

CODE

#include <queue>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#pragma warning (disable:4996)
#pragma warning (disable:6031)
#define mem(a, b) memset(a, b, sizeof a)
using namespace std;
const int N = 220000;
int head[N], nex[N], to[N], edge[N], cnt, _cnt[N], d[N];
bool v[N];
int n, m;
struct SPFA {
	SPFA() {
		mem(head, -1);
		mem(nex, -1);
		mem(_cnt, 0);
		mem(d, 0x3f);
		mem(v, false);
		cnt = 0;
	}
	void init() {
		SPFA();
	}
	void add(int a, int b, int c) {
		++cnt;
		to[cnt] = b;
		edge[cnt] = c;
		nex[cnt] = head[a];
		head[a] = cnt;
	}
	bool spfa() {
		queue<int> q;
		q.push(0);
		d[0] = 0;
		v[0] = 1;
		_cnt[0]++;
		while (!q.empty()) {
			int t = q.front();
			q.pop();
			v[t] = 0;
			for (int i = head[t]; i != -1; i = nex[i]) {
				int y = to[i];
				int dist = edge[i];
				if (d[y] > d[t] + dist) {
					d[y] = d[t] + dist;
					if (!v[y]) {
						v[y] = 1;
						q.push(y);
						_cnt[y]++;
						if (_cnt[y] > n) {
							return false;
						}
					}
				}
			}
		}
		return true;
	}
};
int main()
{
	SPFA sp;
	while (~scanf("%d %d", &n, &m)) {
		sp.init();
		int a, b, c;
		char ch;
		for (int i = 0; i < m; i++) {
			getchar();
			ch = getchar();
			if (ch == 'V') {
				scanf("%d %d", &a, &b);
				sp.add(a, b, -1);
			}
			else {
				scanf("%d %d %d", &a, &b, &c);
				sp.add(b, a, c);
				sp.add(a, b, -c);
			}
		}
		for (int i = 1; i <= n; i++) {
			sp.add(0, i, 0);
		}
		if (sp.spfa()) {
			puts("Reliable");
		}
		else {
			puts("Unreliable");
		}
	}
	return 0;
}

 

Published 143 original articles · won praise 11 · views 8184

Guess you like

Origin blog.csdn.net/weixin_43701790/article/details/104070476