Love Revolution
InterviewTime limit1sMemory limit1024 MB
Remove the cheapest set of unestablished relationships so that no cycle remains, keeping all established ones, and report the minimum total affection lost.
- Level
Medium4 of 10
- Topics
- Minimum spanning tree, Greedy, Union-find
- Solved
- No attempts yet
Problem
Kiryeong, the student council president of Sinchon High School, has been worried about the tangled romances among students lately. Students cannot focus on their studies, so overall grades are falling. To ease their troubled minds, Kiryeong decided to take drastic action and force some of the romantic relationships to be given up. To minimize the fallout, the action must satisfy the following conditions: (1) A relationship that has already been established is not to be given up. (2) The romantic relationships must not form a -gon. A -gon () means that for any distinct students , there is a romantic relationship between and for every , and also between and . (3) If there are several ways to give up relationships, minimize the sum of the affection levels of the relationships given up. However, the number of students is very large, so it is hard to find a way that satisfies these conditions. When the conditions are satisfied, find the minimum sum of the affection levels of the relationships given up.
Input
The first line contains the number of students and the number of romantic relationships . The next lines each contain three integers , , and a status , separated by spaces. Students and have a relationship with affection level . If is 1, the relationship is already established. If is 0, it is not. Any two students are connected by one or more relationships. No pair of students has more than one relationship. No -gon made only of established relationships exists.
Output
Print the minimum sum of the affection levels of the relationships that are given up.