Acwing 1170. Layout (differential constraint)

Keywords: Graph Theory

Acwing 1170. Layout

meaning of the title

Cows like to stand close to their friends when waiting in line for feeding.

Farmer John has N N N cows, number from 1 1 1 to N N N. Stand in a straight line waiting for feeding.

The order of cows in the queue is the same as their numbers.

Because cows are quite slim, there may be two or more cows standing in the same position.

If we imagine cows standing on a number axis, two or more cows are allowed to have the same abscissa.

Some cows like each other, and they want the distance between them not to exceed a given number L L L.

On the other hand, some cows are very disgusted with each other. They want the distance between them to be no less than a given number D D D.

give M L M_L I'll give you a description of the good feelings between the two cows M D M_D MD # a description of disgust between two cows.

Your job is to output if there is no solution that meets the requirements − 1 -1 −1; If 1 1 Cow 1 and N N The distance between N cows can be arbitrarily large, and the output − 2 -2 −2; Otherwise, calculate that if all requirements are met, 1 1 Cow 1 and N N The maximum possible distance between cows N.


Set a set of variables x i x_i xi , indicates the second i i i is the location of cows. Because the title requires cows to stand in the order of numbers, there will be the following inequality constraints:

① x i ≤ x i + 1 x_i \leq x_{i + 1} xi​≤xi+1​

At the same time, according to M L M_L ML} and M D M_D MD ^ has the following inequality constraints

② x b − x a ≤ L x_b - x_a \leq L xb − xa ≤ L where x a ≤ x b x_a \leq x_b xa​≤xb​

③ x b − x a ≥ D x_b - x_a \geq D xb − xa ≥ D where x a ≤ x b x_a \leq x_b xa​≤xb​

Because there is only a relative relationship between cows, there is no absolute relationship, that is, the specific position of a cow on the number axis is not important, just need to meet the constraints of inequality. Because the problem requires the maximum distance, the shortest path model with differential constraints should be used.

Because the problem does not need to find the specific position of each cow, there is no need to add absolute relationship inequality.

First, consider problem 1, which does not meet the requirement, that is, there is a negative ring in the graph.

Question 2 and 3: the distance between cow 1 and cow N can be arbitrarily large, which is equivalent to that the shortest path from 1 to N can be infinitely large. Therefore, find the shortest path from 1 and judge whether d[n] is equal to INF. if = = INF, the distance can be arbitrarily large, otherwise d[n] is the maximum distance.

Consider this question: why do you need to push all points into the queue when judging whether the conditions are met, and only need to push node 1 into the queue when calculating the maximum distance?

  1. Whether the condition is satisfied = = whether there is a negative ring in the graph. If only 1 is pushed into the queue, 1 may not reach all points, so all points should be pushed in

  2. The maximum distance from 1 to N is the classical shortest path problem, so you only need to push the starting point into the queue without considering whether you can reach all points.


// Author: zzqwtcc
// Problem: queue layout
// Contest: AcWing
// Time: 2021-10-29 19:16:45
// URL:
// Memory Limit: 64 MB
// Time Limit: 1000 ms

// #define int long long
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define mod 1000000007
#define MOD 998244353
#define rep(i, st, ed) for (int (i) = (st); (i) <= (ed);++(i))
#define pre(i, ed, st) for (int (i) = (ed); (i) >= (st);--(i))
// #define debug(x,y) cerr << (x) << " == " << (y) << endl;
#define endl '\n'
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
template<typename T> inline T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<typename T> inline T lowbit(T x) { return x & -x; }
template<typename S,typename T>void debug(S s, T t){cerr << s << " == " << t << endl;}
template<typename T>void debug(T t){cerr << t << endl;}
template<typename T>void debug(T t[],int st,int ed){for(int i = st; i <=ed;++i){cerr << t[i] << " ";}cerr << endl;}
template<typename T>void debug(const vector<T>&t){for(int i =0 ; i < t.size();++i)cerr << t[i] << " ";cerr << endl;}
// template<typename T> T qmi(T a, T b = mod - 2, T p = mod) { T res = 1; b %= (p - 1 == 0 ? p : p - 1); while (b) { if (b & 1) { res = (LL)res * a % p; }b >>= 1; a = (LL)a * a % p; }return res % mod; }

const int N = 1010,M = 2e4 + N + 10;
int n;
int m1,m2;
int h[N],e[M],ne[M],w[M],idx;
int cnt[N],d[N];
bool st[N];

void add(int a,int b,int c){
	e[idx] = b,w[idx] = c,ne[idx] = h[a],h[a] = idx++;

bool spfa(int size){
	memset(d,0x3f,sizeof d);
	memset(cnt,0,sizeof cnt);
	memset(st,0,sizeof st);
	for(int i = 1; i <= size;++i){
		d[i] = 0;
		int u = q.front();
		st[u] = false;
		for(int i = h[u];~i;i = ne[i]){
			int j = e[i];
			int dis = w[i];
			if(d[j] > d[u] + dis){
				d[j] = d[u] + dis;
				cnt[j] = cnt[u] + 1;
				if(cnt[j] >= n)return false;
					st[j] = true;
	return true;
void solve() {
    cin >> n >> m1 >> m2;
    memset(h,-1,sizeof h);
    	int a,b,c;scanf("%d%d%d",&a,&b,&c);
    	if(a > b)swap(a,b);
    	int a,b,c;scanf("%d%d%d",&a,&b,&c);
    	if(a > b)swap(a,b);
    for(int i = 1; i <= n - 1;++i){
    	add(i + 1,i,0);
    else {
    	if(d[n] == INF)puts("-2");
    	else printf("%d\n",d[n]);

signed main() {

    //int _; cin >> _;
    //while (_--)

    return 0;

Posted by JayVee on Fri, 29 Oct 2021 05:15:50 -0700