AtCoder Regular Contest 212 (Div. 2)
B
Stones on Grid
AtCoder
⌀ 962
原题
← AC →
翻译中…

Problem Statement

翻译中…

There is a grid with $N$ rows and $N$ columns. The cell at the $i$-th row from the top and $j$-th column from the left is called cell $(i,j)$. You perform the following in order of $i=1,2,\ldots,M$:

翻译中…
  • Operation $i$ : Choose either to place one stone on cell $(x_i, y_i)$ or not. If you place a stone, a cost of $c_i$ is incurred; otherwise, no cost is incurred.
翻译中…

However, you must place a stone in operation $1$.

翻译中…

Determine whether the following goal can be achieved, and if it can, find the minimum sum of costs incurred.

翻译中…
  • Goal : For every $i \ (1 \leq i \leq N)$, the number of stones placed in the $i$-th row is equal to the number of stones placed in the $i$-th column.
翻译中…

Constraints

翻译中…
  • $1 \le N, M \le 2 \times 10^5$
  • $1 \le x_i, y_i \le N$
  • $1 \le c_i \le 10^9$
  • $(x_i, y_i)$ are distinct.
  • All input values are integers.
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format:

翻译中…

N M
x_1 y_1 c_1
x_2 y_2 c_2
\vdots
x_M y_M c_M

翻译中…

Output

翻译中…

If it is impossible to achieve the goal, output -1. If it is possible, output the minimum sum of costs incurred.

翻译中…

样例

Input #1
4 5
2 4 5
4 1 2
2 1 3
3 3 9
1 2 4
Output #1
11
The goal can be achieved by choosing to place a stone in operations $1,2,5$. The sum of costs in this case is $5+2+4=11$. The total cost cannot be less than $11$ to achieve the goal, so the answer is $11$.
Input #2
3 3
1 1 1000000000
2 2 100000000
3 3 10000000
Output #2
1000000000
You must place a stone in operation $1$.
Input #3
2 1
1 2 10
Output #3
-1
题目信息
题号
arc212_b
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。