Differential constraints ------------- queuing layout

When waiting in line for feeding, cows like to stand closer to their friends. Farmer John has NN

Cows, numbered from 11

To NN

, Standing in a straight line waiting to be fed. The order of milk steaks in the line is the same as their number. Because the 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 have a good opinion of each other, they want the distance between the two to not exceed a given number LL

. On the other hand, some cows are very disgusted with each other, they hope that the distance between them is not less than a given number

. Given MLML

A good description about the two cows, and then MDMD

A nasty description of the two cows. Your job is: If there is no solution that meets the requirements, output -1; if 11

Cows and NN

The distance between dairy cows can be arbitrarily large, output -2; otherwise, calculated that all requirements are met, 11

Cows and NN

The largest possible distance between cows. The first line of the input format contains three integers N, ML, MDN, ML, MD

. Next MLML

Rows, each row contains three positive integers A, B, LA, B, L

, Indicating cow AA

With cow BB

At most LL apart

the distance. Then MDMD

Rows, each row contains three positive integers A, B, DA, B, D

, Indicating cow AA

With cow BB

At least DD apart

the distance. The output format outputs an integer, if there is no solution that meets the requirements, output -1; if 11

Cows and NN

The distance between cows can be arbitrarily large, output -2; otherwise, output meets all requirements, 11

Cows and NN

The largest possible distance between cows. Data range 2≤N≤10002≤N≤1000

,
1≤ML,MD≤1041≤ML,MD≤104

,
1≤L,D≤1061≤L,D≤106

Sample input: 4 2 1
1 3 10
2 4 20
2 3 3
Sample output: 27

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010, M = 10000 + 10000 + 1000 + 10, INF = 0x3f3f3f3f;
int n, m1, m2;
int h[N], e[M], w[M], ne[M], idx;
int dist[N];
int q[N], cnt[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)
{
    int hh = 0, tt = 0;
    memset(dist, 0x3f, sizeof dist);
    memset(st, 0, sizeof st);
    memset(cnt, 0, sizeof cnt);
    for (int i = 1; i <= size; i ++ )
    {
        q[tt ++ ] = i;
        dist[i] = 0;
        st[i] = true;
    }
   while (hh != tt)
    {
        int t = q[hh ++ ];
        if (hh == N) hh = 0;
        st[t] = false;
    for (int i = h[t]; ~i; i = ne[i])
        {
            int j = e[i];
            if (dist[j] > dist[t] + w[i])
            {
                dist[j] = dist[t] + w[i];
                cnt[j] = cnt[t] + 1;
                if (cnt[j] >= n) return true;
                if (!st[j])
                {
                    q[tt ++ ] = j;
                    if (tt == N) tt = 0;
                    st[j] = true;
                }
            }
        }
    }
   return false;
}
int main()
{
    scanf("%d%d%d", &n, &m1, &m2);
    memset(h, -1, sizeof h);
  for (int i = 1; i < n; i ++ ) add(i + 1, i, 0);
    while (m1 -- )
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        if (a > b) swap(a, b);
        add(a, b, c);
    }
    while (m2 -- )
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        if (a > b) swap(a, b);
        add(b, a, -c);
    }
   if (spfa(n)) puts("-1");
    else
    {
        spfa(1);
        if (dist[n] == INF) puts("-2");
        else printf("%d\n", dist[n]);
    }
    return 0;
}
164 original articles published · Like 112 · Visitors 6764

Guess you like

Origin blog.csdn.net/qq_45772483/article/details/105500390