#P2916. [USACO08NOV] Cheering up the Cow G

[USACO08NOV] Cheering up the Cow G

Description

Farmer John has NN pastures (numbered 11 to NN, 5N10,0005 \leq N \leq 10,000), each inhabited by one cow. These pastures are connected by PP bidirectional paths (N1P100,000N-1 \leq P \leq 100,000). Each path jj connects pastures SjS_j and EjE_j (1SjN1 \leq S_j \leq N; 1EjN1 \leq E_j \leq N; SjEjS_j \neq E_j), and traversing it takes LjL_j units of time (0Lj1,0000 \leq L_j \leq 1,000). There is at most one direct path between any pair of pastures.

John plans to remove as many roads as possible while keeping all pastures connected. He knows the cows will be very sad after the demolition, so he plans to comfort them afterward. John may choose any pasture to start his comforting tour. After he has visited all cows, he must return to his starting pasture. Each time he passes pasture ii, he must spend CiC_i (1Ci10001 \leq C_i \leq 1000) units of time to talk with the cow there; even if he has already talked before, he must stop and talk again. Note that both when setting out and when returning, he must talk to the cow at the starting pasture.

Assume Farmer John follows your recommendation on which paths to keep, and you also choose the optimal lodging pasture. Compute the minimal total time required while ensuring that each cow is visited at least once.

Input Format

  • Line 11: Two space-separated integers: NN and PP.
  • Lines 22 to N+1N+1: Line i+1i+1 contains one integer: CiC_i.
  • Lines N+2N+2 to N+P+1N+P+1: Line N+j+1N+j+1 contains three space-separated integers: SjS_j, EjE_j, and LjL_j.

Output Format

  • Line 11: A single integer, the minimal total time needed to visit all cows (including the two chats at the lodging pasture).
5 7 
10 
10 
20 
6 
30 
1 2 5 
2 3 5 
2 4 12 
3 4 17 
2 5 15 
3 5 6 
4 5 12 

176 

Hint

   +-(15)-+
  /        \
 /          \
1-(5)-2-(5)-3-(6)--5
   \   /(17)  /
(12)\ /      /(12)
     4------+

Keep these paths:
1-(5)-2-(5)-3      5
       \          /
    (12)\        /(12)
        *4------+

Choose pasture 44 as the lodging pasture, visit in the order 4542321244 \to 5 \to 4 \to 2 \to 3 \to 2 \to 1 \to 2 \to 4, and finally return to sleep. The total time is 176176 units.

Translated by ChatGPT 5