This page is still under construction.

Parts of this page are still being built. What you see may change.

marukaite

Time limit8sMemory limit512 MB

Summary
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×n grid 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)
  • n is the number of cells on one side of the grid Taro made.

  • Wij is the cost of drawing a circle in the cell at row i from the top and column j from the left.

  • Eij is the cost of erasing the circle drawn in the cell at row i from the top and column j from the left.

  • Fi is the initial state of the cells in row i from the top.

  • For the j-th character of Fi from the left:

    • 'o' means a circle is drawn in the cell at row i from the top and column j from the left.
    • '.' means the cell at row i from the top and column j from the left is empty.

Output

mincost
cnt
R1 C1 operate1
R2 C2 operate2
..
Rcnt Ccnt operatecnt
  • mincost is the minimum cost needed to clear Taro's game.

  • mincost is computed as the sum of the costs incurred by write operations and erase operations.

  • cnt : the number of operations performed that achieve the cost mincost

  • The operation performed at the k-th step (1≤k≤cnt) is written on line k+2

  • For the k-th (1≤k≤cnt) operation:

    • If it is performed on the cell at row i from the top and column j from the left,
    • Rkk이다.
    • If this operation erases a circle, set operatek = "erase"
    • If this operation draws a circle, set operatek = "write"
    • Rk,Ck,operatek must be output on one line separated by spaces
  • 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 cnt operations does not match mincost, it is WrongAnswer

Constraints

  • 1≤ n ≤ 100
  • 1≤ Wij ≤ 1000
  • 1≤ Eij ≤ 1000
  • Fi is a string, and its length is n
  • Fi consists only of 'o' and '.'

Examples3

  1. Example 1

    Input
    3
    1 1 1
    1 1 1
    1 1 1
    1 1 1
    1 1 1
    1 1 1
    o.o
    ...
    .o.
    
    Expected output
    2
    2
    1 3 erase
    2 3 write
    
  2. Example 2

    Input
    4
    1 2 3 4
    1 2 3 4
    1 2 3 4
    1 2 3 4
    1 2 3 4
    1 2 3 4
    1 2 3 4
    1 2 3 4
    oooo
    oooo
    oooo
    oooo
    
    Expected output
    30
    12
    1 1 erase
    1 2 erase
    1 3 erase
    2 1 erase
    2 2 erase
    2 4 erase
    3 1 erase
    3 3 erase
    3 4 erase
    4 2 erase
    4 3 erase
    4 4 erase
    
  3. Example 3

    Input
    3
    1 1 1
    1 1 1
    1 1 1
    1 1 1
    1 1 1
    1 1 1
    o..
    .o.
    ..o
    
    Expected output
    0
    0