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的更多相关文章

  1. Codeforces Zepto Code Rush 2014 -C - Dungeons and Candies

    这题给的一个教训:Codeforces没有超时这个概念.本来以为1000*(1000+1)/2*10*10要超时的.结果我想多了. 这题由于k层都可能有关系,所以建一个图,每两个点之间连边,边权为n* ...

  2. Zepto Code Rush 2014——Dungeons and Candies

    题目链接 题意: k个点,每一个点都是一个n * m的char型矩阵.对与每一个点,权值为n * m或者找到一个之前的点,取两个矩阵相应位置不同的字符个数乘以w.找到一个序列,使得全部点的权值和最小 ...

  3. Codeforces 436C

    题目链接 C. Dungeons and Candies time limit per test 2 seconds memory limit per test 256 megabytes input ...

  4. 【POJ2886】Who Gets the Most Candies?-线段树+反素数

    Time Limit: 5000MS Memory Limit: 131072K Case Time Limit: 2000MS Description N children are sitting ...

  5. poj 3159 Candies 差分约束

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 22177   Accepted: 5936 Descrip ...

  6. Who Gets the Most Candies?(线段树 + 反素数 )

    Who Gets the Most Candies? Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d &am ...

  7. poj---(2886)Who Gets the Most Candies?(线段树+数论)

    Who Gets the Most Candies? Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 10373   Acc ...

  8. poj3159 Candies(差分约束,dij+heap)

    poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d ...

  9. HDU 5127 Dogs' Candies

    Dogs' Candies Time Limit: 30000/30000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) T ...

随机推荐

  1. Windows、Ubuntu双系统重装windows系统后grub引导的修复及默认启动项的修改

    今天帮童鞋重装系统,他的电脑是Windows.Ubuntu双系统,需要重装的系统是windows,据说是因为很多游戏都只支持64位,要给换成64位的 = =...      于是我就帮他装了个wind ...

  2. List<T>分组一

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  3. tcp ip参数详解

    http://www.cnblogs.com/digdeep/p/4869010.html 1. TCP/IP模型 我们一般知道OSI的网络参考模型是分为7层:“应表会传网数物”——应用层,表示层,会 ...

  4. 深入分析 Java I/O 的工作机制--转载

    Java 的 I/O 类库的基本架构 I/O 问题是任何编程语言都无法回避的问题,可以说 I/O 问题是整个人机交互的核心问题,因为 I/O 是机器获取和交换信息的主要渠道.在当今这个数据大爆炸时代, ...

  5. docker入门(一)

    docker安装 yum install -y docker-io [root@centos ~]# yum install -y docker-io 已加载插件:fastestmirror, lan ...

  6. 导出到excel

    /// <summary> /// 导出 /// </summary> /// <param name="table">数据表</para ...

  7. uap--studio设置文本字体

  8. .net 使用AjaxControlToolkit.dll 遇到的"Sys"未定义问题

    1.配置文件一般都会缺少<httpHandlers></httpHandlers> 这一段, <httpHandlers> <remove verb=&quo ...

  9. Singleton设计模式的一种见解

    单实例Singleton设计模式可能是被讨论和使用的最广泛的一个设计模式了,这可能也是面试中问得最多的一个设计模式了.这个设计模式主要目的是想在整个系统中只能出现一个类的实例.这样做当然是有必然的,比 ...

  10. 分享整理的sql脚本

    1. 表空间使用率 SQL> select  a.tablespace_name,  2          round(a.total_size) "total_size M" ...