题目描述:
C. Three States
time limit per test5 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard 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
题解:
关键是3个点来联通.3个点来联通的话,如果实在中间相遇,一定会有且仅有一个公共的相遇点. 而在这道题就算是两两建边,也可以看做是有公共点.所以暴力枚举唯一的公共点,然后先bfs预处理出1 2 3 到这个格子用的新建的路数. 注意,bfs不一定路会+1,所以如果不加1我们用双端队列,放在双端队列前面.(用的spfa没有被卡…..)
重点:
3个点的联通公共相遇的话有且只有一个公共点.而不会是一段公共线段.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
const int maxn = 1000+10;
const int INF = 1e9;
int f[4][maxn][maxn];
struct info
{
int x, y;
info(int _x = 0, int _y = 0)
{
x = _x;
y = _y;
}
};
char s[maxn][maxn];
int a[maxn][maxn];
int inq[maxn][maxn];
int n, m;
int check(int x, int y)
{
if(x >= 1 && x<=n&&y>=1&&y<=m)
return 1;
return 0;
}
queue<info> que;
void gao(int key, int f[][maxn])
{
memset(inq, 0, sizeof(inq));
for(int i = 1; i<=n; i++)
{
for(int j = 1; j<=m; j++)
{
f[i][j] = INF;
}
}
while(!que.empty())
{
que.pop();
}
for(int i = 1; i<=n; i++)
{
for(int j = 1; j<=m; j++)
{
if(a[i][j]==key)
{
f[i][j] = 0;
inq[i][j] = 1;
que.push(info(i, j));
}
}
}
while(!que.empty())
{
info t = que.front();
que.pop();
int x = t.x, y = t.y;
inq[x][y] = 0;
for(int k = 0; k<4; k++)
{
int nx = x+dx[k], ny = y+dy[k];
if(check(nx, ny) && a[nx][ny]!=-1)
{
int tmp = f[x][y];
if(a[nx][ny]==0)
{
tmp++;
}
if(tmp < f[nx][ny])
{
f[nx][ny] = tmp;
if(inq[nx][ny]==0)
{
que.push(info(nx, ny));
inq[nx][ny] = 1;
}
}
}
}
}
}
void solve()
{
gao(1, f[1]);
gao(2, f[2]);
gao(3, f[3]);
// for(int k = 1;k<=3;k++)
// {
// for(int i = 1;i<=n;i++)
// {
// for(int j = 1;j<=m;j++)
// {
// printf("%d %d %d -- %d\n", k, i, j, f[k][i][j]);
// }
// }
// }
ll ans = INF;
for(int i = 1;i<=n;i++)
{
for(int j = 1;j<=m;j++)
{
ll tmp = 0;
for(int k = 1;k<=3;k++)
{
tmp += f[k][i][j];
}
if(a[i][j] == 0)
{
tmp -= 2;
}
// if(tmp < ans)
// {
// printf("%d %d --- %d\n", i, j, tmp);
// }
ans = min(tmp, ans);
}
}
if(ans < INF)
printf("%I64d\n", ans);
else
printf("-1\n");
}
int main()
{
//freopen("in.txt", "r", stdin);
while(scanf("%d%d", &n,&m) != EOF)
{
for(int i = 1;i<=n;i++)
{
scanf("%s", s[i]+1);
for(int j = 1;j<=m;j++)
{
if(s[i][j]=='1')
a[i][j] = 1;
if(s[i][j]=='2')
a[i][j] = 2;
if(s[i][j]=='3')
a[i][j] = 3;
if(s[i][j]=='#')
a[i][j] = -1;
if(s[i][j]=='.')
a[i][j] = 0;
}
}
solve();
}
return 0;
}