CodeForces 37E Trial for Chief
Time Limit: 2000MS | Memory Limit: 262144KB | 64bit IO Format: %I64d & %I64u |
Description
Having unraveled the Berland Dictionary, the scientists managed to read the notes of the chroniclers of that time. For example, they learned how the chief of the ancient Berland tribe was chosen.
As soon as enough pretenders was picked, the following test took place among them: the chief of the tribe took a slab divided by horizontal and vertical stripes into identical squares (the slab consisted of N lines and M columns) and painted every square black or white. Then every pretender was given a slab of the same size but painted entirely white. Within a day a pretender could paint any side-linked set of the squares of the slab some color. The set is called linked if for any two squares belonging to the set there is a path belonging the set on which any two neighboring squares share a side. The aim of each pretender is to paint his slab in the exactly the same way as the chief’s slab is painted. The one who paints a slab like that first becomes the new chief.
Scientists found the slab painted by the ancient Berland tribe chief. Help them to determine the minimal amount of days needed to find a new chief if he had to paint his slab in the given way.
Input
The first line contains two integers N and M (1 ≤ N, M ≤ 50) — the number of lines and columns on the slab. The next Nlines contain M symbols each — the final coloration of the slab. W stands for the square that should be painted white and B — for the square that should be painted black.
Output
In the single line output the minimal number of repaintings of side-linked areas needed to get the required coloration of the slab.
Sample Input
3 3
WBW
BWB
WBW
2
2 3
BBB
BWB
1
Source
逆向思维,从目标图开始将图染成初始图。每染一次色,联通块就会扩大,(类似colorflood)。
那么如何计算代价?
从每个点向四周连边,同色代价为0,异色代价为1,O(n^2)枚举起点,跑SPFA,看何时“最远代价最小”
↑注意特判:如果终态染成了全黑的图,因为初始图是全白,所以代价+1
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#define LL long long
using namespace std;
const int mx[]={,,,-,};
const int my[]={,,,,-};
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct edge{
int v,nxt;
int dis;
}e[mxn<<];
int hd[mxn],mct=;
void add_edge(int u,int v,int d){
e[++mct].v=v;e[mct].dis=d;e[mct].nxt=hd[u];hd[u]=mct;return;
}
int n,m;
char mp[][];
int id[][];
bool inq[mxn];
int dis[mxn];
int SPFA(int s){
memset(dis,0x3f,sizeof dis);
queue<int>q;
q.push(s);
inq[s]=;
dis[s]=;
while(!q.empty()){
int u=q.front();q.pop();inq[u]=;
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(dis[v]>dis[u]+e[i].dis){
dis[v]=dis[u]+e[i].dis;
if(!inq[v]){
inq[v]=;
q.push(v);
}
}
}
}
int res=;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
if(mp[i][j]=='W')res=max(res,dis[id[i][j]]);
else res=max(res,dis[id[i][j]]+);
return res;
}
int main()
{
n=read();m=read();
int i,j;
for(i=;i<=n;i++)
scanf("%s",mp[i]+);
for(i=;i<=n;i++)
for(j=;j<=m;j++)
id[i][j]=(i-)*m+j;
for(i=;i<=n;i++)
for(j=;j<=m;j++){
for(int k=;k<=;k++){
int nx=i+mx[k];
int ny=j+my[k];
if(nx< || nx>n || ny< || ny>m)continue;
if(mp[i][j]==mp[nx][ny]){
add_edge(id[i][j],id[nx][ny],);
add_edge(id[nx][ny],id[i][j],);
}
else{
add_edge(id[i][j],id[nx][ny],);
add_edge(id[nx][ny],id[i][j],);
}
}
}
int ans=1e9;
for(i=;i<=n;i++)
for(j=;j<=m;j++){
ans=min(ans,SPFA(id[i][j]));
}
printf("%d\n",ans);
return ;
}
CodeForces 37E Trial for Chief的更多相关文章
- codeforces 37 E. Trial for Chief【spfa】
想象成一层一层的染,所以相邻的两个格子连边,边权同色为0异色为1,然后答案就是某个格子到距离它最远得黑格子的最短距离的最小值 注意特判掉不需要染色的情况 #include<iostream> ...
- [CF] 37 E. Trial for Chief
如果固定了一个中心,那么只需要考虑从它开始最远染到的那些点究竟染了几次. 上下左右不同的点连1边,相同的连0边,跑单源最短路就可以啦. lyd讲的是统计到最远黑点+1的最小值,但是#58数据全是白点, ...
- CF37E Trial for Chief(最短路)
题意 题意是给你一张 NMNMNM 的图,每个点有黑色和白色,初始全为白色,每次可以把一个相同颜色的连续区域染色,求最少的染色次数:(n,m<=50) 题解 转化为最短路.对于每一个点与它相邻的 ...
- Noip前的大抱佛脚----赛前任务
赛前任务 tags:任务清单 前言 现在xzy太弱了,而且他最近越来越弱了,天天被爆踩,天天被爆踩 题单不会在作业部落发布,所以可(yi)能(ding)会不及时更新 省选前的练习莫名其妙地成为了Noi ...
- NOIP前的水题记录
CF147B Smile House 二分+矩阵快速幂,注意一下储存矩阵相乘结果的矩阵,初始化时,a[i][i]=-inf(而其他都可以a[i][i]=0,为了保证答案的可二分性). CF715B C ...
- Educational Codeforces Round 37-E.Connected Components?题解
一.题目 二.题目链接 http://codeforces.com/contest/920/problem/E 三.题意 给定一个$N$和$M$.$N$表示有$N$个点,$M$表示,在一个$N$个点组 ...
- Codeforces Gym 100513G G. FacePalm Accounting
G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513 ...
- Codeforces Gym 100513G G. FacePalm Accounting 暴力
G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513 ...
- Codeforces Bubble Cup 8 - Finals [Online Mirror] D. Tablecity 数学题
D. Tablecity Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/575/problem/D ...
随机推荐
- There is no ‘Animation’ attached to the “Player” game object
There is no ‘Animation’ attached to the “Player” game object 在照着龚老师的Unity3D投篮游戏视频教程练习时,遇到这个错误提示. 我知道 ...
- ASP.NET MVC图片上传前预览
回老家过春节,大半个月,在家的日子里,吃好睡好,人也长了3.5Kg.没有电脑,没有网络,无需写代码,工作上相关的完全放下......开心与父母妻儿过个年,那样的生活令Insus.NET现在还在留恋.. ...
- [转]nodejs npm常用命令
FROM : http://www.cnblogs.com/linjiqin/p/3765772.html npm是一个node包管理和分发工具,已经成为了非官方的发布node模块(包)的标准.有了n ...
- 利用Spring的@Async异步处理改善web应用中耗时操作的用户体验
Web应用中,有时会遇到一些耗时很长的操作(比如:在后台生成100张报表再呈现,或 从ftp下载若干文件,综合处理后再返回给页面下载),用户在网页上点完按钮后,通常会遇到二个问题:页面超时.看不到处理 ...
- flask+sqlite3+echarts2+ajax数据可视化--静态图
结构: /www | |-- /static | | | |-- echarts.js(当然还有echarts原dist目录下的文件(夹)) | |-- /templates | | | |-- in ...
- PHP 对于 MYSQL 基础操作
基础 <?php // 不打印 notice info // error_reporting(0); // 连接 mysql $con = mysql_connect("localho ...
- nios II--实验1——hello_world软件部分
hello_world 软件开发 首先,在硬件工程文件夹里面新建一个software的文件夹用于放置软件部分:打开toolsàNios II 11.0 Software Build Tools for ...
- IIS——发布网站
当我们要上线一个网站时,不要把整个项目原封不动的发布到服务器,而要经过右键发布后,然后再将发布的文件路径配置到IIS~ 详细信息见链接:http://www.52ij.com/jishu/aspx/1 ...
- 37-wc 简明笔记
显示行数.单词数和字节数 wc [options] [file-list] 参数 file-list是wc分析的一个或多个文件的路径名列表.如果省略file-list,wc就从标准输入中读取输入 选项 ...
- make the innerText in the html element can not be selected
approach style="-moz-user-select:none;" onselectstart="javascript:return false;" ...