Water can reach a pasture either by digging a well there (cost $W_i$) or by a pipe from an already-watered pasture (cost $P_{i,j}$). Print the minimum total cost to bring water to all $N$ pastures. (Add a virtual source connected to pasture $i$ with cost $W_i$ and take the minimum spanning tree.)
Input
Line 1: $N$ ($1 \le N \le 300$). Next $N$ lines: $W_i$. Next $N$ lines: the $N \times N$ symmetric pipe-cost matrix $P$.
Output
Print the minimum total watering cost.
Example 1
Input
4 5 4 4 3 0 2 2 2 2 0 3 3 2 3 0 4 2 3 4 0
Output
9
Discussion
Threads about this problem. Spoilers are collapsed; wrap hints in
||double bars||.No discussion yet. Start a thread if something here needs explaining.