【Codeforces Round #327 (Div. 2)E】【最短路】 Three States 块联通

E. Three States
time limit per test
5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

The famous global economic crisis is approaching rapidly, so the states of Berman, Berance and Bertaly formed an alliance and allowed the residents of all member states to freely pass through the territory of any of them. In addition, it was decided that a road between the states should be built to guarantee so that one could any point of any country can be reached from any point of any other State.

Since roads are always expensive, the governments of the states of the newly formed alliance asked you to help them assess the costs. To do this, you have been issued a map that can be represented as a rectangle table consisting of n rows and m columns. Any cell of the map either belongs to one of three states, or is an area where it is allowed to build a road, or is an area where the construction of the road is not allowed. A cell is called passable, if it belongs to one of the states, or the road was built in this cell. From any passable cells you can move up, down, right and left, if the cell that corresponds to the movement exists and is passable.

Your task is to construct a road inside a minimum number of cells, so that it would be possible to get from any cell of any state to any cell of any other state using only passable cells.

It is guaranteed that initially it is possible to reach any cell of any state from any cell of this state, moving only along its cells. It is also guaranteed that for any state there is at least one cell that belongs to it.

Input

The first line of the input contains the dimensions of the map n and m (1 ≤ n, m ≤ 1000) — the number of rows and columns respectively.

Each of the next n lines contain m characters, describing the rows of the map. Digits from 1 to 3 represent the accessory to the corresponding state. The character '.' corresponds to the cell where it is allowed to build a road and the character '#' means no construction is allowed in this cell.

Output

Print a single integer — the minimum number of cells you need to build a road inside in order to connect all the cells of all states. If such a goal is unachievable, print -1.

Sample test(s)
input
4 5
11..2
#..22
#.323
.#333
output
2
input
1 5
1#2#3
output
-1
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T> inline void gmax(T &a,T b){if(b>a)a=b;}
template <class T> inline void gmin(T &a,T b){if(b<a)a=b;}
const int N=1010,M=0,Z=1e9+7,ms63=1061109567;
const int dy[4]={-1,0,0,1},dx[4]={0,-1,1,0};
int n,m;
char s[N][N];
struct BFS
{
	queue<pair<int,int> >q;
	int f[N][N];
	int ans[3];
	void init()
	{
		MS(f,63);
		MS(ans,63);
	}
	void inq(int y,int x,int step)
	{
		if(y<0||y==n||x<0||x==m)return;
		if(s[y][x]=='#')return;
		if(step>=f[y][x])return;
		f[y][x]=step;
		if(isdigit(s[y][x]))
		{
			gmin(ans[s[y][x]-'1'],f[y][x]);
		}
		q.push(MP(y,x));
	}
	void bfs()
	{
		while(!q.empty())
		{
			int y=q.front().first;
			int x=q.front().second;
			q.pop();
			for(int i=0;i<4;i++)
			{
				inq(y+dy[i],x+dx[i],f[y][x]+1);
			}
		}
	}
}a[3];
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		for(int i=0;i<3;i++)a[i].init();
		for(int i=0;i<n;i++)
		{
			scanf("%s",s[i]);
			for(int j=0;s[i][j];j++)if(isdigit(s[i][j]))
			{
				a[s[i][j]-'1'].inq(i,j,0);
			}
		}
		for(int i=0;i<3;i++)a[i].bfs();
		int ans=ms63;
		for(int i=0;i<3;i++)
		{
			gmin(ans,a[i].ans[0]+a[i].ans[1]+a[i].ans[2]-2);
		}
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				LL tmp=-2;
				for(int k=0;k<3;k++)tmp+=a[k].f[i][j];
				if(tmp<ans)ans=tmp;
			}
		}
		if(ans>1e8)ans=-1;
		printf("%d\n",ans);
	}
	return 0;
}
/*
【trick&&吐槽】
比赛的时候因为D题卡太久竟然没有AC这道题QwQ,这题明明是个大水题啊>_<。

trick:关于答案初始量,我很习惯使用==ms63来判定数据非法。但是这题中有-2这个操作,于是这样就可能出错。
	所以最后判定的时候不用==ms63,而是改为>1e8(表示大得过分了)来剔除非法状况。

【题意】
给你一个n(1000)*m(1000)的棋盘,其中有五种类型的格子
'1'、'2'、'3':分别代表1、2、3号国家,每个国家都存在,而且国家内部是以上下左右的方式联通的
'#':障碍物,不可穿越
'.':空地,可以修建道路
我们想要修建尽可能少的道路,使得三个国家都是联通的。

【类型】
最短路

【分析】
这题我们其实只有3个bfs的起点:
1,'1'的所有格子
2,'2'的所有格子
3,'3'的所有格子
然后它们作为原始起点,可以bfs到其它所有格子的最短路。
答案可能是:
1,一个格子,三个国家到这个格子的最短路之和
2,一个国家,其它两个国家到这个国家的距离之和。

总之跑个bfs就可以啦
对于1,gmin(ans,a[0].f[y][x]+a[1].f[y][x]+a[2].f[y][x]-2);
对于2,gmin(ans,a[p].ans[0]+a[p].ans[1]+a[p].ans[2]-2);
上面的-2是什么意思呢?
对于情况1,最后那个格子会被计数*3,然而实际计数*1就够了。
对于情况2,一个块到其它块的距离,会使得这个块的一个格子也被算入了铺路格点,显然也要去掉。

最后检查ans并输出就能AC喽~

【时间复杂度&&优化】
O(nm)

【数据】
Input
1 4
12#3

Output
1061109566

*/


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值