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只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...
随机推荐
- 初识css3 3d动画效果
(先看我博客右上角的3d盒子动画效果,目前没做兼容处理,最好最新的chrome看)无意间看到网上css3写的3d动画效果,实在炫酷,以前理解为需要js去计算去写,没想到css直接可以实现.于是开始研究 ...
- 子类A继承父类B, A a = new A(); 则父类B构造函数、父类B静态代码块、父类B非静态代码块、子类A构造函数、子类A静态代码块、子类A非静态代码块 执行的先后顺序是
按照先后顺序: 1,静态先于非静态代码库执行(静态代码块随着类的加载而加载,初始化只执行一次) 2,父类先于子类 3,非静态代码块优于构造函数执行 所以执行顺序如下: 父类B静态代码块->子类A ...
- tinyproxy 反向代理无法上网原因
今天参照网上教程在服务器安装并配置了tinyproxy反向代理,此次安装反向代理的目的主要是通过内网连接上服务器,再使用服务器作为中转站进行上网.安装并启动的主要步骤如下 下载并安装tinypro ...
- name和value的简单js用法
name和value: 例如: <input type="text" name="txt"/> name用于定义这个input收到的值的变量名,在上 ...
- 【DM8168学习笔记5】EZSDK目录结构
EZSDK5.02的目录结构与之前的版本不同,之前的版本各个组件都放在/ezsdk目录下,5.02做了整合. 之前版本:(图片摘自:3.DM816x_1-day_Workshop-Getting_St ...
- spring源码学习之AOP(一)
继续源码学习,看了spring中基础的容器和AOP感觉自己也没有什么长进,哈哈,我也不知道到底有用没有,这可能是培养自己的一种精神吧,不管那么多,继续学习!AOP中 AOP中几个重要的概念:(1)Ad ...
- tyvj 1423 GF和猫咪的玩具
传送门 解题思路 题目比较水,floyd求出最短路取个最小值即可.结果joyoi时限写错了..好像只有0ms才能过??突然发现加了快读就T不加就A,数据在10000以下的还是scanf快啊. 代码 # ...
- js图片碎片效果(移动端也适用)
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 用reduce装逼 之 多个数组中得出公共子数组,统计数组元素出现次数
昨天做了一道美团的面试题,要求是给N个数组,找出N个数组的公共子数组. ,,,,]; ,,,,]; ,,,,]; ,,,,]; 以上四个数组,有公共子数组2, 3,7 function main(){ ...
- 读书笔记--Head First C#目录
1.c#助你快速开发2.都只是代码3.对象4.类型与引用5.封装6.继承7.接口与抽象类8.枚举与集合9.读/写文件10.异常处理11.事件与委托12.复习与预习13.控件与图片14.captain ...