CF436C.Dungeons and Candies

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

During the loading of the game "Dungeons and Candies" you are required to get descriptions of kk levels from the server. Each description is a map of an n×mn×m checkered rectangular field. Some cells of the field contain candies (each cell has at most one candy). An empty cell is denoted as "." on the map, but if a cell has a candy, it is denoted as a letter of the English alphabet. A level may contain identical candies, in this case the letters in the corresponding cells of the map will be the same.

When you transmit information via a network, you want to minimize traffic — the total size of the transferred data. The levels can be transmitted in any order. There are two ways to transmit the current level AA :

  1. You can transmit the whole level AA . Then you need to transmit nmn·m bytes via the network.
  2. You can transmit the difference between level AA and some previously transmitted level BB (if it exists); this operation requires to transmit dA,Bwd_{A,B}·w bytes, where dA,Bd_{A,B} is the number of cells of the field that are different for AA and BB , and ww is a constant. Note, that you should compare only the corresponding cells of levels AA and BB to calculate dA,Bd_{A,B} . You cannot transform the maps of levels, i.e. rotate or shift them relatively to each other.

Your task is to find a way to transfer all the kk levels and minimize the traffic.

输入格式

The first line contains four integers n,m,k,wn,m,k,w (1<=n,m<=10; 1<=k,w<=1000)(1<=n,m<=10; 1<=k,w<=1000) . Then follows the description of kk levels. Each level is described by nn lines, each line contains mm characters. Each character is either a letter of the English alphabet or a dot ("."). Please note that the case of the letters matters.

输出格式

In the first line print the required minimum number of transferred bytes.

Then print kk pairs of integers x1,y1,x2,y2,...,xk,ykx_{1},y_{1},x_{2},y_{2},...,x_{k},y_{k} , describing the way to transfer levels. Pair xix_{i} , yiy_{i} means that level xix_{i} needs to be transferred by way yiy_{i} . If yiy_{i} equals 0, that means that the level must be transferred using the first way, otherwise yiy_{i} must be equal to the number of a previously transferred level. It means that you will transfer the difference between levels yiy_{i} and xix_{i} to transfer level xix_{i} . Print the pairs in the order of transferring levels. The levels are numbered 1 through kk in the order they follow in the input.

If there are multiple optimal solutions, you can print any of them.

输入输出样例

  • 输入#1

    2 3 3 2
    A.A
    ...
    A.a
    ..C
    X.Y
    ...
    

    输出#1

    14
    1 0
    2 1
    3 1
    
  • 输入#2

    1 1 4 1
    A
    .
    B
    .
    

    输出#2

    3
    1 0
    2 0
    4 2
    3 0
    
  • 输入#3

    1 3 5 2
    ABA
    BBB
    BBA
    BAB
    ABB
    

    输出#3

    11
    1 0
    3 1
    2 3
    4 2
    5 1
    
首页