Codeforces 436 C. Dungeons and Candies

本文深入探讨了游戏开发领域的关键技术,包括游戏引擎、Unity、Cocos2d-X等,以及AI音视频处理的应用,如语义识别、物体检测、语音识别等。同时,文章还涉及了开发工具、大数据开发、测试等方面的内容,为开发者提供全面的技术指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


MST....

C. Dungeons and Candies
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

During the loading of the game "Dungeons and Candies" you are required to get descriptions of k levels from the server. Each description is a map of an n × 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 A:

  1. You can transmit the whole level A. Then you need to transmit n·m bytes via the network.
  2. You can transmit the difference between level A and some previously transmitted level B (if it exists); this operation requires to transmit dA, B·w bytes, where dA, B is the number of cells of the field that are different for A and B, and w is a constant. Note, that you should compare only the corresponding cells of levels A and B to calculate dA, 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 k levels and minimize the traffic.

Input

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

Output

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

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

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

Sample test(s)
input
2 3 3 2
A.A
...
A.a
..C
X.Y
...
output
14
1 0
2 1
3 1
input
1 1 4 1
A
.
B
.
output
3
1 0
2 0
4 2
3 0
input
1 3 5 2
ABA
BBB
BBA
BAB
ABB
output
11
1 0
3 1
2 3
4 2
5 1

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

struct Edge
{
    int u,v,w;
}edge[1100*1100];

bool cmp(Edge a,Edge b)
{
    return a.w<b.w;
}

int n,m,k,w;
int fa[1100],diff[1100][1100];
char ch[1100][20][20];
vector<int> vc[1100];

int get_diff(char a[][20],char b[][20] )
{
    int ret=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(a[i][j]!=b[i][j])
                ret++;
        }
    }
    return ret;
}

int getfa(int x)
{
    if(x==fa[x]) return x;
    else return fa[x]=getfa(fa[x]);
}

int kruscal(int eg)
{
    sort(edge,edge+eg,cmp);
    for(int i=0;i<=k+1;i++)
    {
        fa[i]=i;
        vc[i].clear();
    }
    int cnt=k+1,ans=0;
    for(int i=0;i<eg;i++)
    {
        int f1=getfa(edge[i].u);
        int f2=getfa(edge[i].v);
        if(f1!=f2)
        {
            fa[f1]=f2;
            ans+=edge[i].w;
            vc[edge[i].u].push_back(edge[i].v);
            vc[edge[i].v].push_back(edge[i].u);
            cnt--;
            if(cnt==1) break;
        }
    }
    return ans;
}

void dfs(int u,int fa)
{
    for(int i=0,sz=vc[u].size();i<sz;i++)
    {
        int v=vc[u][i];
        if(v==fa) continue;
        printf("%d %d\n",v,u);
        dfs(v,u);
    }
}

int main()
{
    scanf("%d%d%d%d",&n,&m,&k,&w);
    for(int i=1;i<=k;i++)
    {
        for(int j=0;j<n;j++)
        {
            scanf("%s",ch[i][j]);
        }
    }
    for(int i=1;i<=k;i++)
    {
        for(int j=i+1;j<=k;j++)
        {
            diff[j][i]=diff[i][j]=get_diff(ch[i],ch[j])*w;
        }
    }
    int eg=0;
    for(int i=1;i<=k;i++)
    {
        edge[eg++]=(Edge){0,i,n*m};
        for(int j=i+1;j<=k;j++)
        {
            edge[eg++]=(Edge){i,j,diff[i][j]};
        }
    }
    printf("%d\n",kruscal(eg));
    dfs(0,0);
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值