[返回模拟退火略解]

题目描述

在一个 n×mn\times mn×m 的矩阵中,每个点都染了一种颜色(只能是 [1,c][1,c][1,c] 中的一种),求一种方案,使得相邻异色点对数最小。

Solution 3936\text{Solution 3936}Solution 3936

这是一道好题。正所谓代码五分钟,调参两百年。

随机一个矩阵作为初始矩阵。每次降温时尝试交换两个随机元素,判断交换后是否更优。详见代码。

对于每次交换,更新 calccalccalc 函数可以在 O(1)O(1)O(1) 中完成(提示:一个元素仅能与其上下左右相邻的元素产生贡献,仅更新交换的两个元素即可)。这部分请读者自行完成推导。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm> #define reg register int n,m,c;
int p[60];
struct node{
int a[30][30];
}o,ans;
int answ=0x3f3f3f3f; int calc(node a){
int cnt=0;
for(reg int i=1;i<=n;++i)
for(reg int j=1;j<=m;++j){
if(i>=2&&a.a[i][j]!=a.a[i-1][j]) ++cnt;
if(i<=n-1&&a.a[i][j]!=a.a[i+1][j]) ++cnt;
if(j>=2&&a.a[i][j]!=a.a[i][j-1]) ++cnt;
if(j<=n-1&&a.a[i][j]!=a.a[i][j+1]) ++cnt;
}
return cnt/2;
}
void SA(){
double t=1.0;
while(t>1e-15){
node no=ans;
int x1,y1,x2,y2,tt;
do{
x1=rand()%n+1;
y1=rand()%m+1;
x2=rand()%n+1;
y2=rand()%m+1;
}while(x1==x2&&y1==y2);
tt=no.a[x1][y1];no.a[x1][y1]=no.a[x2][y2];no.a[x2][y2]=tt;
int nw=calc(no);
int delta=nw-answ;
if(delta<0){
answ=nw;
ans=o=no;
}
else if(exp(-delta/t)*32767>rand()) o=no;
t*=0.9999;
}
}
void work(){
int now=1,pt=1,xx=1,yy=1; //随便搞一个矩阵作为初始矩阵
for(reg int i=1;i<=n*m;++i){
o.a[xx][yy]=now;
if(yy==m) yy=0,++xx;
if(pt>=p[now]){
pt=0;
++now;
}
++yy;++pt;
}
o.a[n][m]=c;ans=o;
for(reg int i=1;i<=10;++i) SA();
}
int main(){
srand(1007);
scanf("%d%d%d",&n,&m,&c);
for(reg int i=1;i<=c;++i)
scanf("%d",&p[i]);
work();
for(reg int i=1;i<=n;++i){
for(reg int j=1;j<=m;++j)
printf("%d ",ans.a[i][j]);
puts("");
}
// printf("%d",calc(ans));
}

luogu P3936 Coloring的更多相关文章

  1. 题解 洛谷P3936 Coloring

    考虑搜索,发现复杂度爆炸        贪心,正确性过低(~~实测爆炸~~) 于是,~~发现~~这题是模拟退火 这里不讲解退火的定义了,初学退火可以去平衡点 退火本身维护一个答案图像,答案的q,当前图 ...

  2. Luogu 魔法学院杯-第二弹(萌新的第一法blog)

    虽然有点久远  还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题  沉迷游戏,伤感情 #include <queue> ...

  3. luogu p1268 树的重量——构造,真正考验编程能力

    题目链接:http://www.luogu.org/problem/show?pid=1268#sub -------- 这道题费了我不少心思= =其实思路和标称毫无差别,但是由于不习惯ACM风格的题 ...

  4. Codeforces Round #369 (Div. 2)---C - Coloring Trees (很妙的DP题)

    题目链接 http://codeforces.com/contest/711/problem/C Description ZS the Coder and Chris the Baboon has a ...

  5. CF149D. Coloring Brackets[区间DP !]

    题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数 区间DP 用栈先处理匹配 f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数 l和r匹配的话,转移到(l+1,r-1 ...

  6. Codeforces Round #369 (Div. 2) C. Coloring Trees DP

    C. Coloring Trees   ZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the pa ...

  7. CodeForces #369 C. Coloring Trees DP

    题目链接:C. Coloring Trees 题意:给出n棵树的颜色,有些树被染了,有些没有.现在让你把没被染色的树染色.使得beauty = k.问,最少使用的颜料是多少.   K:连续的颜色为一组 ...

  8. CodeForces 149D Coloring Brackets

    Coloring Brackets time limit per test: 2 seconds memory limit per test: 256 megabytes input: standar ...

  9. C. Coloring Trees DP

    传送门:http://codeforces.com/problemset/problem/711/C 题目: C. Coloring Trees time limit per test 2 secon ...

随机推荐

  1. Winform中对ZedGraph的RadioGroup进行数据源绑定,即通过代码添加选项

    场景 在寻找设置RadioGroup的选项时没有找到相关博客,在DevExpress的官网找到 怎样给其添加选项. DevExpress官网教程: https://documentation.deve ...

  2. Hbase 日常运维

    日常维护的命令 1,major_compact 'testtable',通常生产环境会关闭自动major_compact(配置文件中hbase.hregion.majorcompaction设 为0) ...

  3. maven下载jar包源码配置

    两个依赖,就想下mail的源码包,因该怎么 <dependencies> <dependency> <groupId>javax.mail</groupId& ...

  4. Java抽象类构造方法

    java中抽象类的子类的构造方法会隐含父类的无参构造方法. package com.zempty.abstractclass; public class AbstractDemo01 { public ...

  5. Find a way(两个BFS)

    Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. L ...

  6. Kali Linux 安装open-vm-tools

    Kali Linux是基于Debian的Linux发行版,集成了精心挑选的渗透测试和安全审计的工具,供渗透测试和安全设计人员使用.(以及一些各种颜色的hacker  ^-^) 首先需要安装好虚拟机(V ...

  7. linux切换jdk

    一.安装openjdk yum search openjdk yum install java-1.8.0-openjdk-devel-debug.x86_64 二.查询java版本 alternat ...

  8. Vue学习之todolist功能开发

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Hadoop入门 之 Hadoop常识

    1.Hadoop是什么? 答:Hadoop是开源的分布式存储和分布式计算平台. 2.Hadoop的组成是什么? 答:Hadoop由HDFS和MapReduce这两个核心部分组成. HDFS(Hadoo ...

  10. jupyter编辑快捷键

    Jupyter笔记本有两种不同的键盘输入模式. 编辑模式允许您将代码或文本输入到一个单元格中,并通过一个绿色的单元格来表示 命令模式将键盘与笔记本级命令绑定在一起,并通过一个灰色的单元格边界显示,该边 ...