Fair Meeting
Time limit2sMemory limit1024 MB
Fill in the unknown integer distances so that no triangle has a unique smallest edge, minimizing the total, or report -1 if impossible.
- Level
Hard8 of 10
- Topics
- Graph, Union-find, Greedy
- Solved
- No attempts yet
Problem
In the distant future, SNUPS has grown into a large club of members, driven by a boom in algorithm education. Any two distinct members can be assigned a closeness distance, an integer where a smaller value means the two are closer. For all with , and .
Each time an agenda item comes up, SNUPS picks three of the members at random to hold the meeting. When two of the three who are close join forces, the third member's opinion is not reflected. Concretely, if the three distinct members picked are , , and , and and , then member 's opinion is not reflected. Such a meeting is called unfair.
Jeyeon wants to make sure no unfair meeting can happen for any three members. So far, only pairs of members know each other. The distances of known pairs cannot change, but Jeyeon can set the distances of unknown pairs as she likes. The final distances must still be integers within range and symmetric.
If such a plan is feasible, the goal is to minimize . Determine whether the plan is feasible, and if it is, find the minimum value of this sum.
Input
The first line contains and . Each of the next lines contains three integers , , and , meaning members and already know each other and their closeness distance is .
Output
Print -1 if the plan cannot be carried out. Otherwise, print the minimum value of .
Constraints
- , , and for