#P3275. [SCOI2011] 糖果

    ID: 2324 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>2011四川各省省选最短路差分约束

[SCOI2011] 糖果

Description

There are NN children in a kindergarten. Teacher lxhgww\text{lxhgww} wants to distribute candies to them, and every child must receive at least one candy. However, the children can be jealous and may raise some requests. For example, Xiao Ming does not want Xiao Hong to receive more candies than he does. Therefore, when distributing candies, lxhgww\text{lxhgww} needs to satisfy KK requests from the children. Since the total number of candies is limited, lxhgww\text{lxhgww} wants to know the minimum number of candies he needs to prepare so that every child receives candies and all requests are satisfied.

Input Format

The first line contains two integers N,KN, K. The next KK lines each contain 33 integers X,A,BX, A, B, describing a request from the children.

  • If X=1X=1, the AA-th child must receive the same number of candies as the BB-th child.
  • If X=2X=2, the AA-th child must receive fewer candies than the BB-th child.
  • If X=3X=3, the AA-th child must receive no fewer candies than the BB-th child.
  • If X=4X=4, the AA-th child must receive more candies than the BB-th child.
  • If X=5X=5, the AA-th child must receive no more candies than the BB-th child.

Output Format

Output a single integer: the minimum number of candies lxhgww\text{lxhgww} needs to prepare. If it is impossible to satisfy all the requests, output 1-1.

5 7
1 1 2
2 3 2
4 4 1
3 4 5
5 4 5
2 3 5
4 5 1
11

Hint

For 30%30\% of the testdata, N100N \leq 100.

For 100%100\% of the testdata, 1N,K1051 \leq N, K \leq 10^5, 1X51 \leq X \leq 5, 1A,BN1 \leq A, B \leq N.


upd 2022.7.6\text{upd 2022.7.6}: Added 2121 sets of Hack testdata.

Translated by ChatGPT 5