Dungeons and Candies
Zepto Code Rush 2014:http://codeforces.com/problemset/problem/436/C
题意:k个点,每个点都是一个n * m的char型矩阵。对与每个点,权值为n * m或者找到一个之前的点,取两个矩阵对应位置不同的字符个数乘以w。找到一个序列,使得所有点的权值和最小。
题解:很明显的最小生成树。但是要加入一个0点,边权为n*m,其余k个点两两建立一条边,边权是diff[i][j]*w,最后这一题,我要死掉的地方就是输出,不仅要输出费用,还要输出边,但是这里的边,看了半天,才知道,要按dfs序列输出,并且第一个点的前一个点必须是0.哎,这一题,只能说明自己太渣了。有点伤心。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m,k,w,num,cost,ct;
int fa[];
int g[][];
void init(){
for(int i=;i<=k;i++)
fa[i]=i;
}
int Find(int x){
int s;
for(s=x;s!=fa[s];s=fa[s]);
while(s!=x){
int temp=fa[x];
fa[x]=s;
x=temp;
}
return s;
}
struct Node{
int x;
int y;
int val;
}edge[];
int cmp1(Node a,Node b){
return a.val<b.val;
}
int cmp2(Node a,Node b){
return a.y<b.y;
}
char mp[][][];
void deal(){
for(int i=;i<=k;i++){
for(int j=i+;j<=k;j++){
int counts=;
for(int g=;g<=n;g++){
for(int h=;h<=m;h++){
if(mp[i][g][h]!=mp[j][g][h])
counts++;
}
}
edge[++num].x=i;
edge[num].y=j;
edge[num].val=counts*w;
}
}
for(int i=;i<=k;i++){
edge[++num].x=;
edge[num].y=i;
edge[num].val=n*m;
}
}
void print(int u,int fa){
for(int i=;i<=k;i++){
if(g[u][i]>=&&i!=fa){
printf("%d %d\n",i,u);
print(i,u);
}
} }
int main(){
while(~scanf("%d%d%d%d",&n,&m,&k,&w)){
init();
memset(g,-,sizeof(g));
for(int i=;i<=k;i++){
for(int j=;j<=n;j++){
for(int h=;h<=m;h++)
cin>>mp[i][j][h];
}
}
num=;
deal();
sort(edge+,edge+num+,cmp1);
ct=;cost=;
for(int i=;i<=num;i++){
int x=Find(edge[i].x);
int y=Find(edge[i].y);
if(x!=y){
fa[x]=y;
int tx=edge[i].y;
int ty=edge[i].x;
g[tx][ty]=g[ty][tx]=edge[i].val;
ct++;
cost+=edge[i].val;
}
if(ct==k)break;
}
printf("%d\n",cost);
print(,);
}
}
Dungeons and Candies的更多相关文章
- Codeforces Zepto Code Rush 2014 -C - Dungeons and Candies
这题给的一个教训:Codeforces没有超时这个概念.本来以为1000*(1000+1)/2*10*10要超时的.结果我想多了. 这题由于k层都可能有关系,所以建一个图,每两个点之间连边,边权为n* ...
- Zepto Code Rush 2014——Dungeons and Candies
题目链接 题意: k个点,每一个点都是一个n * m的char型矩阵.对与每一个点,权值为n * m或者找到一个之前的点,取两个矩阵相应位置不同的字符个数乘以w.找到一个序列,使得全部点的权值和最小 ...
- Codeforces 436C
题目链接 C. Dungeons and Candies time limit per test 2 seconds memory limit per test 256 megabytes input ...
- 【POJ2886】Who Gets the Most Candies?-线段树+反素数
Time Limit: 5000MS Memory Limit: 131072K Case Time Limit: 2000MS Description N children are sitting ...
- poj 3159 Candies 差分约束
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 22177 Accepted: 5936 Descrip ...
- Who Gets the Most Candies?(线段树 + 反素数 )
Who Gets the Most Candies? Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%I64d &am ...
- poj---(2886)Who Gets the Most Candies?(线段树+数论)
Who Gets the Most Candies? Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 10373 Acc ...
- poj3159 Candies(差分约束,dij+heap)
poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d ...
- HDU 5127 Dogs' Candies
Dogs' Candies Time Limit: 30000/30000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) T ...
随机推荐
- Android自定义drawable(Shape)详解
在Android开发过程中,经常需要改变控件的默认样式, 那么通常会使用多个图片来解决.不过这种方式可能需要多个图片,比如一个按钮,需要点击时的式样图片,默认的式样图片. 这样就容易使apk变大. 那 ...
- Java语言基础(三)
Java语言基础(三) 一. 补码 (1).之所以有补码是因为要考虑成本 就是造计算机的成本 (2).下面让我们分析一下补码 以四位补码为例 <1> 高位是符号位,它决定其是正数还是 ...
- Configuring Robolectric
There are numerous ways to customize how Robolectric behaves at runtime. Config Annotation The prima ...
- Topcoder SRM 661 (Div.1) 250 MissingLCM - 数论
[题意] 给你一个数N(1<=N<=10^6),要求最小的M(M>N),使得lcm(n+1,n+2,...m)=lcm(1,2,3,...,m) [思路] 手速太慢啦,等敲完代码的时 ...
- cogs 2507 零食店
/* cogs 2507 零食店 跪了这题的数据了.... 第一遍Q*m 暴力询问 嗯 以为能的70 但只有40 Q已经到了1e6了 考试的时候 放弃了第三题又打了一遍 这次是Q*(n+logn) 最 ...
- 关于ADO.NET的一些知识整理
ADO.NET是什么 虽然我们都知道ADO.NET是对数据库的操作,但是要真的说出ADO.NET的具体含义还不是很容易. ADO.NET是ActiveX Data Objects的缩写,它是一个COM ...
- 2016年11月1日——jQuery源码学习笔记
1.instanceof运算符希望左操作数是一个对象,右操作数标识对象的类.如果左侧的对象是右侧类的实例,则表达式返回true,否则返回false 2.RegExp.exec() 如果 exec() ...
- servlet 项目 ,,启动没问题,,但是,一请求也面就报错误。。。。求解决。。。。。。。。。。。。。各种百度,都没解决了啊。。。。。急急急急急急急急急急急急急急急急急急
信息: Server startup in 1674 mslog4j:WARN No appenders could be found for logger (com.mchange.v2.log.M ...
- Linux下安装Nginx1.9.3-0303(本人亲手实践)
Linux下安装Nginx1.9.3 Linux操作系统 Oel 5.8 64bit 最新版Nginx: 1.9.3 最近同事让我帮忙搞 ngix,两天时间 安装.配置搞定了.继续 Nginx 1.9 ...
- [总结]FFMPEG视音频编解码零基础学习方法
在CSDN上的这一段日子,接触到了很多同行业的人,尤其是使用FFMPEG进行视音频编解码的人,有的已经是有多年经验的“大神”,有的是刚开始学习的初学者.在和大家探讨的过程中,我忽然发现了一个问题:在“ ...