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. Spring DI模式 小样例

           今儿跟同事讨论起来spring早期的,通过大篇幅xml的配置演变到今天annotation的过程,然后随手写了个小样例,感觉还不错,贴到这里留个纪念. 样例就是用JAVA API的方式, ...

  2. ExtJs4学习(四):Extjs 中id与itemId的差别

       为了方便表示或是指定一个组件的名称,我们一般会使用id或者itemId进行标识命名. (推荐尽量使用itemId.这样能够降低页面唯一标识而产生的冲突) id:   id是作为整个页面的Comp ...

  3. 为什么我选择使用 Blocks(块)

    扯淡:到了新公司接手新框架之后,发现大量的使用Blocks,之前很多时候都是使用代理,突然面对这个陌生的语法,特地科普总结了一番. 什么是Blocks 一句话概括就是,带有局部变量的匿名函数(即不带名 ...

  4. 从高德 SDK 学习 Android 动态加载资源

    前不久跑去折腾高德 SDK 中的 HUD 功能,相信用过该功能的用户都知道 HUD 界面上的导航转向图标是动态变化的.从高德官方导航 API 文档中 AMapNaviGuide 类的描述可知,导航转向 ...

  5. APP切图那些事儿-Android ios

  6. Codeforces 570D - Tree Requests【树形转线性,前缀和】

    http://codeforces.com/contest/570/problem/D 给一棵有根树(50w个点)(指定根是1号节点),每个点上有一个小写字母,然后有最多50w个询问,每个询问给出x和 ...

  7. Http,Https (SSL)的Url绝对路径,相对路径解决方案Security Switch 4.2 英文帮助文档 分类: ASP.NET 2014-10-28 10:50 147人阅读 评论(1) 收藏

    Security Switch 4.2 =================== Security Switch enables various ASP.NET applications to auto ...

  8. document.documentElement.style判断浏览器是否支持Css3属性

    1.document.documentElement.style 属性定义了当前浏览器支持的所有Css属性 包括带前缀的和不带前缀的 例如:animation,webkitAnimation,msAn ...

  9. java中XMLGregorianCalendar类型和Date类型之间的相互转换

    import java.text.SimpleDateFormat;import java.util.Date;import java.util.GregorianCalendar;import ja ...

  10. PHP 一维数组排序

    一.按照键值从低到高排序,并赋予新的键名 1 函数:sort( array &array[, int sort_flags]) 2 参数: (1)&array : 要排序的一维数组, ...