题目链接:
题目
E. Three States
time limit per test:5 seconds
memory limit per test:512 megabytes
问题描述
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.
输入
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.
输出
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.
样例
input
4 5
11..2
#..22
#.323
.#333
output
2
input
1 5
1#2#3
output
-1
题意
1,2,3代表三个州,'.'可以修路,'#'不可以修路,让你建最少的路把三个州连通起来,(数据保证每个州都是连通的)
题解
对三个州分别跑一遍最短路,然后枚举每个点,计算以这个点为公共节点的路的最小花费。
注意:由于有连通块,跑最短路的时候普通的bfs是错的,需要用dijkstra之类的最短路算法去跑,如果用优先队列+bfs跑的话,一定要松弛!(因为有长度为0的边!)其实这样也就是相当于是dijkstra了。
错误代码:(最短路算法出错了)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define X first
#define Y second
#define mp make_pair
using namespace std;
const int maxn = 1000 + 10;
int INF = 0x3f3f3f3f;
char str[maxn][maxn];
int d[maxn][maxn][3];
bool inq[maxn][maxn][3];
typedef __int64 LL;
int n, m;
struct Heap {
int x, y, d;
Heap(int x, int y, int d) :x(x), y(y), d(d) {}
bool operator < (const Heap& tmp) const {
return d > tmp.d;
}
};
const int dx[] = { -1,1,0,0 };
const int dy[] = { 0,0,-1,1 };
void bfs(pair<int, int> s, int k) {
priority_queue<Heap> Q;
d[s.X][s.Y][k] = 0;
Q.push(Heap(s.X,s.Y,0));
while (!Q.empty()) {
Heap u = Q.top(); Q.pop();
for (int i = 0; i<4; i++) {
int nx = u.x + dx[i], ny = u.y + dy[i];
if (nx<0 || nx >= n || ny<0 || ny >= m) continue;
if (str[nx][ny] == '#'||d[nx][ny][k]<INF) continue;
if (str[nx][ny] != str[u.x][u.y] || (str[nx][ny]==str[u.x][u.y])&&str[nx][ny] == '.') {
//这里要加松弛!!!
d[nx][ny][k] = d[u.x][u.y][k] + 1;
Q.push(Heap(nx,ny,d[nx][ny][k]));
}
else {
d[nx][ny][k] = d[u.x][u.y][k];
Q.push(Heap(nx,ny,d[nx][ny][k]));
}
}
}
}
int main() {
scanf("%d%d", &n, &m);
memset(d, 0x3f, sizeof(d)); INF = d[0][0][0];
memset(inq, 0, sizeof(inq));
for (int i = 0; i<n; i++) scanf("%s", str[i]);
for (int i = 0; i<n; i++) {
for (int j = 0; j<m; j++) {
if (str[i][j] >= '1'&&str[i][j] <= '3') {
int ch = str[i][j] - '1';
if (d[i][j][ch] >= INF) {
bfs(mp(i, j), ch);
}
}
}
}
//for (int k = 0; k < 3; k++) {
// for (int i = 0; i<n; i++) {
// for (int j = 0; j<m; j++) {
// if (d[i][j][k] >= INF) printf("-1 ");
// else printf("%02d ", d[i][j][k]);
// }
// puts("");
// }
// puts("\n");
//}
int ans = INF;
for (int i = 0; i<n; i++) {
for (int j = 0; j<m; j++) {
if (str[i][j] == '#') continue;
if (d[i][j][0] >= INF || d[i][j][1] >= INF || d[i][j][2] >= INF) continue;
ans = min((LL)ans, (LL)d[i][j][0] + d[i][j][1] + d[i][j][2] - 2);
}
}
if(ans<INF) printf("%d\n", ans);
else puts("-1");
return 0;
}
正解:(dijkstra)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define X first
#define Y second
#define mp make_pair
using namespace std;
const int maxn = 1000 + 10;
const int INF = 1e7;
char str[maxn][maxn];
int d[maxn][maxn][3];
int n, m;
struct Heap {
int x, y, d;
Heap(int x, int y, int d) :x(x), y(y), d(d) {}
bool operator < (const Heap& tmp) const {
return d > tmp.d;
}
};
const int dx[] = { -1,1,0,0 };
const int dy[] = { 0,0,-1,1 };
int done[maxn][maxn];
void bfs(int xs,int ys, int k) {
memset(done,0,sizeof(done));
priority_queue<Heap> Q;
d[xs][ys][k] = 0;
Q.push(Heap(xs,ys,0));
while (!Q.empty()) {
Heap u = Q.top();
Q.pop();
if(done[u.x][u.y]) continue;
done[u.x][u.y]=1;
for (int i = 0; i<4; i++) {
int nx = u.x + dx[i], ny = u.y + dy[i];
if (nx<0 || nx >= n || ny<0 || ny >= m) continue;
if (str[nx][ny] == '#') continue;
if (str[nx][ny]==str[u.x][u.y]&&str[u.x][u.y]!='.') {
if(d[nx][ny][k] > d[u.x][u.y][k]) {
d[nx][ny][k] = d[u.x][u.y][k];
Q.push(Heap(nx,ny,d[nx][ny][k]));
}
} else {
if(d[nx][ny][k] >d[u.x][u.y][k] + 1) {
d[nx][ny][k] = d[u.x][u.y][k] + 1;
Q.push(Heap(nx,ny,d[nx][ny][k]));
}
}
}
}
}
int main() {
for (int i = 0; i < maxn; i++) {
for (int j = 0; j < maxn; j++) {
for (int k = 0; k < 3; k++) {
d[i][j][k] = INF;
}
}
}
scanf("%d%d", &n, &m);
for (int i = 0; i<n; i++) scanf("%s", str[i]);
for (int i = 0; i<n; i++) {
for (int j = 0; j<m; j++) {
if (str[i][j] >= '1'&&str[i][j] <= '3') {
int ch = str[i][j] - '1';
if (d[i][j][ch] >= INF) {
bfs(i,j, ch);
}
}
}
}
int ans = INF;
for (int i = 0; i<n; i++) {
for (int j = 0; j<m; j++) {
if (str[i][j] == '#') continue;
if (d[i][j][0] >= INF || d[i][j][1] >= INF || d[i][j][2] >= INF) continue;
ans = min(ans, d[i][j][0] + d[i][j][1] + d[i][j][2] - 2);
}
}
if(ans<INF) printf("%d\n", ans);
else puts("-1");
return 0;
}