题目链接

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 ;
}





												

Codeforces 436C的更多相关文章

  1. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  2. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  3. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  4. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  5. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  6. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  7. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  8. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

  9. CodeForces - 148D Bag of mice

    http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...

随机推荐

  1. Odoo Documentation : Fields

    Fields Basic fields class openerp.fields.Field(string=None, **kwargs) The field descriptor contains ...

  2. 模板:ST表

    ST表:解决RMQ类问题,预处理$O(nlog_{2}n)$,查询$O(1)$ 较线段树来说每次查询为1,线段树为log,但ST表不方便更改 ST表还用了倍增思想. 模板: struct ST_MAP ...

  3. Django项目:CRM(客户关系管理系统)--73--63PerfectCRM实现CRM讲师下载作业

    # teacher_urls.py # ————————62PerfectCRM实现CRM讲师讲课记录———————— from django.conf.urls import url from bp ...

  4. Django项目:CRM(客户关系管理系统)--59--49PerfectCRM实现CRM客户报名流程学生合同表单验证

    # sales_views.py # ————————47PerfectCRM实现CRM客户报名流程———————— from django.db import IntegrityError #主动捕 ...

  5. 各ui库项目结构

    饿了么的ui组件库 sass build:webpack配置文件 examples: element api的页面文档 packages: 放置组件  css放在./theme-chalk 下 src ...

  6. java记录CST时间类型的字符串转换问题

    下列代码可以实现把Java的CST格式的时间字符串转为为Date对象和所需要的日期时间格式! String dateStr = "Wed Sep 16 11:26:23 CST 2009&q ...

  7. valueStack(值栈)

    值栈 值栈(ValueStack)就是 OGNL 表达式存取数据的地方.在一个值栈中,封装了一次请求所需要的所有数据. 在使用 Struts2 的项目中,Struts2 会为每个请求创建一个新的值栈, ...

  8. 关于Git回退再前进造成本地代码和远程仓库代码不一致的问题

    事情经过:  git push 提交之后(版本2.0), 回退, 然后做了一些修改, 发现有问题,于是脑抽回退git reset --hard HEAD^ (版本1,0), 然后又前进到之前那个版本( ...

  9. NOSQL数据库之 REDIS

    NOSQL数据库之 REDIS 一.NOSQL 1.简介 NoSQL ,(Not Only SQL),泛指非关系型数据库. 特点: NoSQL 通常是以key-value形式存储, 不支持SQL语句, ...

  10. ios h5 出现的问题

    这几天在测试的时候,忽然发现手机ios 页面中的input 样式出现问题,安卓就没事. 实际应该是第一张图,在ios中出现的结果为第二张图    出现这个原因,主要是ios系统自带的设置,解决方法为 ...