"Explanations" Luo Valley P1993 small farms K

Better reading experience

Portal

Portal1: Luogu

Description

Small \ (K \) in (\ mathrm MC \) \ build many, many farms inside, a total of \ (n \) months, that he had forgotten the exact number of each farm planted crops, and he only remembers vague information (co \ (m \) a), is described in the following three forms:

  • Farm \ (A \) than farm \ (B \) at least one more planted \ (C \) crop units,

  • Farm \ (A \) than farm \ (B \) to a lot planted \ (C \) crop units,

  • Farm \ (A \) and farm \ (B \) as many as the number of crop cultivation.

However, due to the small \ (K \) some deviation of memory, so he wants to know it exists or not a situation in which the number of farms growing crops with all the information he remembered match.

Input

The first row comprises two integers \ (n-\) and \ (m \) , represent the number of farms and small \ (K \) number of information memory.

Next \ (m \) line:

If the first number of each line is the \ (1 \) , then there is \ (3 \) integers \ (A, B, C \) , represents farm \ (A \) than farm \ (B \) at least multi-planted \ (c \) crop units.

If the first number of each line is the \ (2 \) , then there is \ (3 \) integers \ (A, B, C \) , represents farm \ (A \) than farm \ (B \) to lot planted \ (c \) crop units. If the first number of each line is the \ (3 \) , then there is \ (2 \) integers \ (A, B \) , represents farm \ (A \) number and grown \ (B \) as much.

Output

If there is a situation with small \ (K \) memory match, output Yes, or output No.

Sample Input

3 3
3 1 2
1 1 3 1
2 2 3 2

Sample Output

Yes

Hint

For \ (100 \% \) of the guaranteed data: \ (. 1 \ n-Le, m, A, B, C \ 10000 Le \) .

Solution

We \ (\ mathrm {s [i ]} \) represents \ (I \) number of crop farms, we then subject the conditions can be expressed as:

  1. \(s[a] \ge s[b] + c\)

  2. \(s[b] \ge s[a] - c\)

  3. \(s[b] = s[a]\)

Because if we want this question with differential constraints do, put all the constraints are changed to \ (\ le \) or (\ ge \) \ form, but do all the number of crop farms this question can not be is negative, it is necessary to use the longest way to solve. Therefore, we can read:

  1. \(s[a] \ge s[b] + c\)

  2. \(s[b] \ge s[a] - c\)

  3. \(s[a] \ge s[b] + 0\)

  4. \(s[b] \ge s[a] + 0\)

  5. \(s[i] \ge 0\)

Then we start to build side:

For the given topic \ (a, b, c \ )

  1. \ (b \ to a \) build a weight value \ (C \) side;

  2. \ (a \ to b \) build a weight value \ (- c \) side;

  3. \ (a \ to b \) build a weight value \ (0 \) side;

  4. \ (b \ to a \) key a weight value \ (0 \) side;

Finally \ (0 \ to i, i \ in [1, n] \) to build a weight of \ (0 \) side.

After completing construction with \ (\ mathrm {SPFA} \ ) run again the longest path, the way it is determined ring.

Code

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>

using namespace std;

const int INF = 0x3f3f3f3f, MAXN = 20005;
struct EDGE {
    int to, nxt, val;
} edge[MAXN];
int n, m, u, v, opt, val, cnt, tot[MAXN], vis[MAXN], dis[MAXN], head[MAXN];
inline void addedge(int u, int v, int val) {//邻接表存图
    edge[++cnt].to = v; edge[cnt].val = val; edge[cnt].nxt = head[u]; head[u] = cnt;
}
inline bool SPFA() {//SPFA最长路
    priority_queue<int> Q;
    memset(dis, -INF, sizeof(dis));
    vis[0] = 1;
    dis[0] = 0;
    tot[0] = 1;
    Q.push(0);
    while (!Q.empty()) {
        int u = Q.top();
        Q.pop();
        vis[u] = 0;
        for (int i = head[u]; ~i; i = edge[i].nxt) {
            int v = edge[i].to;
            if (dis[v] < dis[u] + edge[i].val) {
                dis[v] = dis[u] + edge[i].val;
                if (!vis[v]) {
                    tot[v] = tot[u] + 1;
                    Q.push(v);
                    vis[v] = 1;
                    if (tot[v] > n) return 0;
                }
            }
        }
    }
    return 1;
}
int main() {
    scanf("%d%d", &n, &m);
    memset(head, -1, sizeof(head));
    for (int i = 1; i <= m; i++) {
        scanf("%d", &opt);
        if (opt == 1) {
            scanf("%d%d%d", &u, &v, &val);
            addedge(u, v, -val);
        } else
        if (opt == 2) {
            scanf("%d%d%d", &u, &v, &val);
            addedge(v, u, val);
        } else {
            scanf("%d%d", &u, &v);
            addedge(u, v, 0);
            addedge(v, u, 0);
        }
    }
    for (int i = 1; i <= n; i++)
        addedge(0, i, 0);//按题目的描述建边
    if (SPFA()) printf("Yes\n"); else printf("No\n");
    return 0;
}

Guess you like

Origin www.cnblogs.com/shenxiaohuang/p/11405020.html