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 ...
随机推荐
- 在RichTextBox控件中插入图片
. 在RichTextBox控件中插入图片 关键点 . 实现过程 . public void ShowInsertImageDlg() { OpenFileDialog OpenFileD ...
- CMake高速入门
入门基础:http://www.ibm.com/developerworks/cn/linux/l-cn-cmake/ 在 linux 下使用 CMake 构建应用程序 入门进阶:http ...
- 学习Oracle应该准备的技能
首先数据库方面的技能:数据库体系结构.基本概念和基本理论.数据库管理.备份.恢复.SQL开发.PLSQL开发.工具的使用等等. 操作系统方面的技能:对Linux/Unix系统应该有相当的基础.理解RA ...
- PHP面向对象之旅:模板模式(转)
抽象类的应用就是典型的模版模式 抽象类的应用就是典型的模版模式,先声明一个不能被实例化的模版,在子类中去依照模版实现具体的应用. 我们写这样一个应用: 银行计算利息,都是利率乘以本金和存款时间,但各种 ...
- 原创翻译:蓝牙(BLE)for iOS
About Core Bluetooth 简要:核心蓝牙框架提供了iOS和MAC 应用程序与BLE 设备进行无线通信所需要的类.通过该框架,应用程序可以扫描.发现BLE 外设,如心率.电子温度传感器等 ...
- 动态拼接 sql的时候 里面 如果有变量的话 按上面的方式进行处理
set @Sql_Sql = N' select top 1 @m_zw=zw,@m_zh=temp from ket where zd=''ddd'' ' print @Sql_Sql EXEC s ...
- 关于EF查询表里的部分字段
这个在项目中用到了,在网上找了一下才找到,留下来以后自已使用. List<UniversalInfo> list =new List<UniversalInfo>(); lis ...
- PHP 解决时差8小时的问题
有时候用php echo date("Y-m-d H:i:s")的时候会发现自己的时间和系统时间有差别 这里问题一般就是因为你自己的时区和配置的时区出现了差别的原因: 解决办法有三 ...
- html multiple select option 分组
普通html方式展示<select name="viewType" style="width: 100%;height: 300px;" multiple ...
- OpenWrt启动过程分析
openwrt是通过一系列shell脚本进行启动流程的组织,下面是启动流程的提纲.如 果想详细了解启动的过程,则需要仔细走读脚本文件. 1. 在make menuconfig 选择target平台 B ...