Zepto Code Rush 2014——Dungeons and Candies
- 题意:
k个点,每一个点都是一个n * m的char型矩阵。对与每一个点,权值为n * m或者找到一个之前的点,取两个矩阵相应位置不同的字符个数乘以w。找到一个序列,使得全部点的权值和最小 - 分析:
首先,这个图是一个无向图。求权值和最小,每一个权值相应的是一条边,且每一个点仅仅能有一个权值即一条边,一个k个边,和生成树非常像,可是须要证明不能有环形。最好还是如果如今有三个点,每一个点的最小边成环,这时候是不能找到一个序列使得每一个点都取到它的最小边值的,所以,k个点k个边不能有环且边值和最小,就是最小生成树。
const int maxn = 1100; char ipt[maxn][11][11];
int dist[maxn][maxn];
int d[maxn], p[maxn];
bool vis[maxn];
int n, m, k, w; int main()
{
// freopen("in.txt", "r", stdin);
while (~RIV(n, m, k, w))
{
CLR(dist, 0);
REP(i, k)
{
vis[i] = false;
d[i] = n * m;
p[i] = -1;
} REP(i, k) REP(j, n)
RS(ipt[i][j]);
REP(i, k) REP(j, k) REP(ii, n) REP(jj, m)
dist[i][j] += (ipt[i][ii][jj] != ipt[j][ii][jj]) * w;
d[0] = 0;
int sum = n * m;
VI ans;
REP(i, k)
{
int M = INF, ind;
REP(j, k)
if (!vis[j] && d[j] < M)
{
ind = j;
M = d[j];
}
vis[ind] = true;
sum += M;
ans.push_back(ind);
REP(j, k)
{
if (!vis[j] && dist[ind][j] < d[j])
{
d[j] = dist[ind][j];
p[j] = ind;
}
}
}
WI(sum);
REP(i, ans.size())
{
cout << ans[i] + 1 << ' ' << p[ans[i]] + 1 << endl;
}
}
return 0;
}
kruskal:
const int maxn = 1100; struct Edge
{
int from, to, dist;
int operator< (const Edge& rhs) const
{
return dist < rhs.dist;
}
Edge (int from = 0, int to = 0, int dist = 0)
{
this->from = from;
this->to = to;
this->dist = dist;
}
}; vector<Edge> G[maxn];
int in[maxn];
vector<Edge> edges;
int fa[maxn];
char ipt[maxn][105];
int diff[maxn][maxn];
int n, m, k, w;
int find(int n)
{
return (n == fa[n]) ? n : (fa[n] = find(fa[n]));
}
void init(int n)
{
REP(i, n)
{
fa[i] = i;
G[i].clear();
in[i] = 0;
}
edges.clear();
}
void AddEdge(int u, int v, int dist)
{
edges.push_back(Edge(u, v, dist));
}
void dfs(int u, int fa)
{
REP(i, G[u].size())
{
Edge& e = G[u][i];
if (e.to != fa)
{
if (e.dist == m * n)
cout << e.to + 1 << ' ' << 0 << endl;
else
cout << e.to + 1 << ' ' << u + 1 << endl;
dfs(e.to, u);
}
}
}
void solve()
{
int ret = 0;
sort(all(edges));
REP(i, edges.size())
{
Edge& e = edges[i];
int ru = find(e.from), rv = find(e.to);
if (ru != rv)
{
fa[ru] = rv;
ret += e.dist;
G[e.from].push_back(Edge(e.from, e.to, e.dist));
G[e.to].push_back(Edge(e.to, e.from, e.dist));
in[e.from]++;
in[e.to]++;
}
}
WI(ret + n * m);
REP(i, k)
{
if (in[i] <= 1)
{
cout << i + 1 << ' ' << 0 << endl;
dfs(i, -1);
break;
}
}
} int judge(int a, int b)
{
int cnt = 0;
REP(i, n * m)
cnt += (ipt[a][i] != ipt[b][i]);
return cnt;
} int main()
{
// freopen("in.txt", "r", stdin);
while (~RIV(n, m, k, w))
{
CLR(diff, 0);
REP(i, k)
{
int len = 0;
REP(j, n)
{
RS(ipt[i] + len);
len = strlen(ipt[i]);
}
}
REP(i, k) REP(j, k) REP(t, n * m)
diff[i][j] += (ipt[i][t] != ipt[j][t]);
init(k);
REP(i, k) REP(j, k)
{
if (i == j)
continue;
AddEdge(i, j, min(diff[i][j] * w, n * m));
}
solve();
}
return 0;
}
Zepto Code Rush 2014——Dungeons and Candies的更多相关文章
- Codeforces Zepto Code Rush 2014 -C - Dungeons and Candies
这题给的一个教训:Codeforces没有超时这个概念.本来以为1000*(1000+1)/2*10*10要超时的.结果我想多了. 这题由于k层都可能有关系,所以建一个图,每两个点之间连边,边权为n* ...
- CF Zepto Code Rush 2014 B. Om Nom and Spiders
Om Nom and Spiders time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- Zepto Code Rush 2014 A. Feed with Candy
此题用贪心求解, 首先将caramel drop类别的糖果按照高度从小到大排序,如果高度相同,按照重量从小到大排序 将fruit drop类别的糖果按照高度从小到大排序,如果高度相同,按照重量从小到大 ...
- Zepto Code Rush 2014 B - Om Nom and Spiders
注意题目给的是一个nxm的park,设元素为aij,元素aij 有4种可能U(上移),D(下移),L(左移),R(右移) 假设第i行第j列元素aij(注意元素的索引是从0开始的) 当aij为D时,此时 ...
- Zepto Code Rush 2014-A. Feed with Candy(HACK)
A. Feed with Candy time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Dungeons and Candies
Zepto Code Rush 2014:http://codeforces.com/problemset/problem/436/C 题意:k个点,每个点都是一个n * m的char型矩阵.对与每个 ...
- Code Rush插件
code rush 是微软推出的一款VS2008上的插件.他有强大的文件和代码导航功能,易于访问的重构和代码创建功能.一组编辑器.选择.剪贴板工具等. 教程链接 http://www.devexpre ...
- ZeptoLab Code Rush 2015 C. Om Nom and Candies 暴力
C. Om Nom and Candies Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/526 ...
- ZeptoLab Code Rush 2015 C. Om Nom and Candies [ 数学 ]
传送门 C. Om Nom and Candies time limit per test 1 second memory limit per test 256 megabytes input sta ...
随机推荐
- DICOM医学图像处理:开源库mDCM与DCMTK的比較分析(一),JPEG无损压缩DCM图像
背景介绍: 近期项目需求,须要使用C#进行最新的UI和相关DICOM3.0医学图像模块的开发.在C++语言下,我使用的是应用最广泛的DCMTK开源库,在本专栏的起初阶段的大多数博文都是对DCMTK开源 ...
- python获取的信息列表微信公共平台和用户头像
转载注明原文地址:http://blog.csdn.net/btyh17mxy/article/details/25207889 只写模拟登陆的方式获取微信从信息和头像库列表公共平台, - 相关后,功 ...
- 怎样配置git ssh连接,怎样在GitHub上加入协作开发人员,怎样配置gitignore和怎样在GitHub上删除资源库.
**********1.在运行git push origin master指令时报例如以下错误: iluckysi@ILUCKYSI-PC /d/ilucky/message/code (master ...
- 排序算法门外汉理解-Shell排序
#include <stdio.h> /* 希尔排序 基本思想:希尔排序又称为缩小增量排序,对简单插入排序的优化. (外部分组gap,组内部插入排序! ! ) 特点:一种不稳定的排序 */ ...
- 同ListView该接口无法通过手势滑动左右切换界面问题解决方法
同ListView该接口无法通过手势滑动左右切换界面问题解决方法 问题描写叙述: 在做OnGestureListener滑动切换窗体的时候,会遇到这种问题.就是当界面中含有ListView的时候.On ...
- 文件搜索神器everything 你不知道的技巧总结
everything这个软件用了很久,总结了一些大家可能没注意到的技巧,分享给大家 1.指定文件目录搜索示例: TDDOWNLOAD\ abc 在所有TDDOWNLOAD文件夹下搜索包含 ...
- 简单vector达到
得知c++于,看完这本书后,,最近苦于不知道下一步该怎么做了,在寻找STL在各种容器的源代码分析,我想一次又一次地实现它. 之前,很多问题看的时候不知道是怎么回事,意与理解的.这个vector类写得特 ...
- WebStorm的compass配置
在webstorm中配置compass WebStorm是功能强大的前端开发专用IDE,拥有即时编辑(chrome).自动完成.debugger.Emmet.HTML5 支持.JSLint.Less. ...
- web自定义炫酷字体
电脑有已经安装好的字体,但是如果你有特殊需要而要选择其他字体,则需要以下几个步骤 1.寻找适合你的字体 有下面几个站点提供字体下载: www.theleagueofmoveabletype.com w ...
- tomcat配置sqlserver数据库
1. 首先确保Tomcat安装文件夹中的\common\lib(对于Tomcat5.5)或者是\lib(Tomcat6.0)文件夹中已包括JDBC连接数据库所必须的三个.jar文件(msbase.ja ...