题目链接

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 xi, yi 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
解题思路: 以第i个level为结点i, 并增加一个0作为采用第一种方式上传level, 每个点(1...k)到标记为0的点的边权为n * m, 
      其他从i点到j点的边权为diff(level i, level j), 也就是第i个level和第j个level不同的个数,
      然后求一遍MST(最小生成树)即可。
Accepted Code:
 /*************************************************************************
> File Name: 436C.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年06月24日 星期二 17时03分36秒
> Propose:
************************************************************************/ #include <cmath>
#include <string>
#include <cstdio>
#include <vector>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; #define INF (0x3f3f3f3f)
int n, m, k, w;
char a[][][];
int cost[][], d[], from[];
bool used[];
vector<int> OoO; int
dist(int x, int y) {
int cnt = ;
for (int i = ; i < n; i++)
for (int j = ; j < m; j++)
if (a[x][i][j] != a[y][i][j]) cnt++;
return cnt;
} int
main(void) {
ios_base::sync_with_stdio(false);
while (~scanf("%d %d %d %d", &n, &m, &k, &w)) {
for (int i = ; i <= k; i++)
for (int j = ; j < n; j++) scanf("%s", a[i][j]); for (int i = ; i <= k; i++) {
for (int j = i+; j <= k; j++) {
cost[i][j] = cost[j][i] = dist(i, j) * w;
}
cost[i][] = cost[][i] = m * n;
}
memset(used, false, sizeof(used));
memset(d, 0x3f, sizeof(d));
d[] = ;
int ans = ;
OoO.clear();
while (true) {
int v = -;
for (int u = ; u <= k; u++) {
if (!used[u] && (v==- || d[v] > d[u])) {
v = u;
}
}
if (v == -) break;
used[v] = true;
ans += d[v];
for (int u = ; u <= k; u++) if (!used[u] && d[u] > cost[u][v]){
d[u] = cost[u][v];
from[u] = v;
}
OoO.push_back(v);
}
printf("%d\n", ans);
for (int i = ; i <= k; i++) {
printf("%d %d\n", OoO[i], from[OoO[i]]);
}
} return ;
}





												

最新文章

  1. CentOS 7搭建SVN服务器
  2. 使用 SWFObject.js 插入Flash
  3. Elasticsearch 教程--分布式集群
  4. Go - 数组 和 切片(array、slice)
  5. 对java多线程的认识
  6. java.lang.OutOfMemoryError: PermGen space及其解决方法(转载)
  7. ELK部署
  8. 【poj3693】Maximum repetition substring(后缀数组+RMQ)
  9. JS子元素oumouseover触发父元素onmouseout
  10. UE4实现闪烁效果
  11. 细说logback之简介
  12. 扩展的GM命令
  13. 漏洞复现——httpd换行解析漏洞
  14. [视频播放] HLS协议之M3U8、TS流详解
  15. LOJ#2799. 「CCC 2016」生命之环
  16. 【BFS】【map】hdu5925 Coconuts
  17. Linux 运维之硬链接与软链接详解
  18. @Modules( ... ) 多个包路径问题
  19. codeforces 110E Lucky Tree
  20. Tomcat Bug记录

热门文章

  1. [编织消息框架][netty源码分析]3 EventLoop 实现类SingleThreadEventLoop职责与实现
  2. 阿里云安全研究成果入选人工智能顶级会议 IJCAI 2019, 业界首次用AI解决又一难题!
  3. html常用标签详解2-图片标签详解
  4. LUOGU P3157 [CQOI2011]动态逆序对(CDQ 分治)
  5. 路由的配置,侧边栏类名与url的结合运用
  6. Object源码阅读
  7. mysqldump与mydumper
  8. CentOS + Nginx 的常用操作指令总结
  9. TZ_16_Vue的v-for、v-if、v-show、v-bind、watch
  10. phpstudy安装好之后mysql无法启动(亲测可行)