#P2210. [USACO13OPEN] Haywire B

    ID: 1183 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>动态规划,dp搜索USACO模拟退火

[USACO13OPEN] Haywire B

Description

Farmer John has NN cows (4N124 \leq N \leq 12, and NN is even). They set up a homemade system that allows cows and their friends to communicate through lines protected by hay bales.

Each cow in the pasture has exactly 33 friends, and the cows must be arranged in a single row of hay bales.

A line of length LL occupies exactly LL hay bales to protect it. For example, if two cows are placed at hay bales 44 and 77 and they are friends, then we need 33 hay bales to build the line that connects them.

Assume that every pair of friends must be connected by a separate line, and we may rearrange the cows arbitrarily. Compute the minimum total number of hay bales required to build all the lines.

Input Format

Line 11: An integer NN. For convenience, the cows are numbered 1N1 \sim N.

Lines 2,3,,N+12, 3, \dots, N + 1: Each line contains three integers in 1N1 \sim N. The numbers on line i+1i + 1 are the indices of cow ii’s three friends. Obviously, if cow ii is one of cow jj’s three friends, then cow jj is also one of cow ii’s three friends.

Output Format

A single integer representing the minimum number of hay bales required to build the lines.

6
6 2 5
1 3 4
4 2 6
5 3 2
4 6 1
1 5 3
17

Hint

Sample explanation: the best arrangement of the cows is {6,5,1,4,2,3}\{6, 5, 1, 4, 2, 3\}, in which case we need only 1717 hay bales.

Translated by ChatGPT 5