Codeforces 436C
C. Dungeons and Candiestime limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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:
- You can transmit the whole level A. Then you need to transmit n·m bytes via the network.
- 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.
InputThe 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.
OutputIn 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)Input2 3 3 2
A.A
...
A.a
..C
X.Y
...Output14
1 0
2 1
3 1Input1 1 4 1
A
.
B
.Output3
1 0
2 0
4 2
3 0Input1 3 5 2
ABA
BBB
BBA
BAB
ABBOutput11
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的更多相关文章
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
- CodeForces - 148D Bag of mice
http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...
随机推荐
- 如何通过EditPlus远程连接Linux
1. File - FTP - FTP Settings 2. Add 3. 填写Linux的ip地址及用户名和密码 4. OK
- 在Ubuntu Kylin 16.04 LTS 上源码方式安装odoo
更新Ubuntu服务器软件源 运行以下代码: sudo apt-get update #更新软件源 sudo apt-get dist-upgrade #更新软件包,自动查找依赖关系 sudo s ...
- List--使用List作为堆栈和队列
1,List作为堆栈 堆栈“先进后出”.对此,可以使用append和pop来操作数据. 不指定下标时,pop会先操作最后一个数据. 例如: 2,队列 队列“先进先出”.当然也可以使用append和po ...
- 基于宜搭的《T恤尺码收集》应用搭建
简介: 在阿里,T恤是程序员必不可少的元素.每逢公司或者BU(部门)的重大节庆日,比如双11 .年会.新BU成立仪式.大型活动等,都会给员工定制发放统一的T恤或者POLO衫服装.而我们每次发放T恤之前 ...
- LUOGU P3157 [CQOI2011]动态逆序对(CDQ 分治)
传送门 解题思路 cdq分治,将位置看做一维,修改时间看做一维,权值看做一维,然后就转化成了三维偏序,用排序+cdq+树状数组.注意算删除贡献时要做两次cdq,分别算对前面和后面的贡献. #inclu ...
- 20191005 - New Beginning
真·反思 Before 发现$T1$是约瑟夫,$T2$不清楚,$T3$是算法进阶上的$LCIS$ During得&失 做的不错的地方: 多少想了T1的优化(最后没打完). T3的暴力写得很快也 ...
- mysql高级教程(一)-----逻辑架构、查询流程、索引
mysql逻辑架构 和其它数据库相比,MySQL有点与众不同,它的架构可以在多种不同场景中应用并发挥良好作用.主要体现在存储引擎的架构上,插件式的存储引擎架构将查询处理和其它的系统任务以及数据的存储提 ...
- KOA 学习(二)
app.listen(...) Koa 应用并非是一个 1-to-1 表征关系的 HTTP 服务器. 一个或多个Koa应用可以被挂载到一起组成一个包含单一 HTTP 服务器的大型应用群. var ko ...
- js 中直接调用和new的区别
var test = new Test(); // 这里的 test 是什么? 是一个 Test 对象吗?错!这里 test 是一个函数——Test 中返回的 function() { return ...
- 2019-8-31-C++-驱动开发-error-LNK2019-unresolved-external-symbol-__CheckForDebuggerJustMyCode-referenced-...
title author date CreateTime categories C++ 驱动开发 error LNK2019 unresolved external symbol __CheckFor ...