marukaite
Time limit8sMemory limit512 MB
Given costs to draw and erase circles on an n by n grid, find a minimum-cost sequence of operations that leaves exactly one circle in every row and every column, and output the operations.
- Level
Medium7 of 10
- Topics
- Minimum spanning tree, Graph, Greedy, Implementation
- Solved
- No attempts yet
Problem
Taro is an elementary school student, doodling on the back of a flyer. One day, Taro came up with the following game.
- Draw an
n×ngrid of cells. - The initial state of each cell is either a circle is drawn or it is not drawn.
- The goal is to erase and draw circles so that, in the end, every column contains exactly one circle and every row contains exactly one circle. Reaching this state clears the game.
Taro came up with this game, but clearing it takes him a very long time. So he asked you, a university student, for help. You are Taro's older brother and a university student, and your job is as follows. To consider the situation precisely, you have worked out the cost of drawing a circle in a cell and the cost of erasing a circle in a cell. Using these costs, consider a sequence of operations that minimizes the cost of clearing this game. At this point, write a program that outputs the minimum cost and a sequence of operations that achieves that cost. For the output, any operations and any order that achieve the minimum cost may be output.
Input
n
W11 W12 .. W1n
W21 W22 .. W2n
..
Wn1 Wn2 .. Wnn
E11 E12 .. E1n
E21 E22 .. E2n
..
En1 En2 .. Enn
F1(n characters)
F2(n characters)
..
Fn(n characters)
-
nis the number of cells on one side of the grid Taro made. -
Wijis the cost of drawing a circle in the cell at rowifrom the top and columnjfrom the left. -
Eijis the cost of erasing the circle drawn in the cell at rowifrom the top and columnjfrom the left. -
Fiis the initial state of the cells in rowifrom the top. -
For the
j-th character ofFifrom the left:- 'o' means a circle is drawn in the cell at row
ifrom the top and columnjfrom the left. - '.' means the cell at row
ifrom the top and columnjfrom the left is empty.
- 'o' means a circle is drawn in the cell at row
Output
mincost
cnt
R1 C1 operate1
R2 C2 operate2
..
Rcnt Ccnt operatecnt
-
mincostis the minimum cost needed to clear Taro's game. -
mincostis computed as the sum of the costs incurred by write operations and erase operations. -
cnt: the number of operations performed that achieve the costmincost -
The operation performed at the
k-th step(1≤k≤cnt)is written on linek+2 -
For the
k-th(1≤k≤cnt)operation:- If it is performed on the cell at row
ifrom the top and columnjfrom the left, Rkk이다.- If this operation erases a circle, set
operatek= "erase" - If this operation draws a circle, set
operatek= "write" Rk,Ck,operatekmust be output on one line separated by spaces
- If it is performed on the cell at row
-
Performing an operation that draws a circle in a cell that already has a circle, or an operation that erases a circle in a cell that does not have a circle, is WrongAnswer
-
If the sum of the costs of the
cntoperations does not matchmincost, it is WrongAnswer
Constraints
1≤ n ≤ 1001≤ Wij ≤ 10001≤ Eij ≤ 1000Fiis a string, and its length isnFiconsists only of 'o' and '.'