Codeforces Round #327 (Div. 2) E. Three States
题目链接:
题目
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;
}
Codeforces Round #327 (Div. 2) E. Three States的更多相关文章
- Codeforces Round #327 (Div. 2) E. Three States BFS
E. Three States Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/probl ...
- 暴搜 - Codeforces Round #327 (Div. 2) E. Three States
E. Three States Problem's Link Mean: 在一个N*M的方格内,有五种字符:'1','2','3','.','#'. 现在要你在'.'的地方修路,使得至少存在一个块'1 ...
- Codeforces Round #327 (Div. 1) C. Three States
C. Three States time limit per test 5 seconds memory limit per test 512 megabytes input standard inp ...
- E. Three States - Codeforces Round #327 (Div. 2) 590C States(广搜)
题目大意:有一个M*N的矩阵,在这个矩阵里面有三个王国,编号分别是123,想知道这三个王国连接起来最少需要再修多少路. 分析:首先求出来每个王国到所有能够到达点至少需要修建多少路,然后枚举所有点求出来 ...
- Codeforces Round #327 (Div. 2) A. Wizards' Duel 水题
A. Wizards' Duel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/prob ...
- Codeforces Round #327 (Div. 2) D. Chip 'n Dale Rescue Rangers 二分 物理
D. Chip 'n Dale Rescue Rangers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/co ...
- Codeforces Round #327 (Div. 2) C. Median Smoothing 找规律
C. Median Smoothing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/p ...
- Codeforces Round #327 (Div. 2) B. Rebranding 水题
B. Rebranding Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/problem ...
- Codeforces Round #327 (Div. 1), problem: (A) Median Smoothing
http://codeforces.com/problemset/problem/590/A: 在CF时没做出来,当时直接模拟,然后就超时喽. 题意是给你一个0 1串然后首位和末位固定不变,从第二项开 ...
随机推荐
- 解决ashx文件下的Session“未将对象引用设置到对象的实例”
using System; using System.Collections.Generic; using System.Linq; using System.Web; using PPT_DAL; ...
- iOS中调用系统录音功能及其播放
最近做的项目中,用到了录音的功能,简单记录一下. 我的想法是:通过重写button的点击事件,来达到录音的目的. /*----------------------------------[录音]--- ...
- 转载:简单介绍Python中的try和finally和with方法
用 Python 做一件很平常的事情: 打开文件, 逐行读入, 最后关掉文件; 进一步的需求是, 这也许是程序中一个可选的功能, 如果有任何问题, 比如文件无法打开, 或是读取出错, 那么在函数内需要 ...
- EDK中如何使用ISE中生成的IP
EDK中如何使用ISE中生成的IP: 网上上有说这个的文章,但是很复杂,也就是添加bdd文件,其实这些都不需要自己操作的,我们可以在EDK中import 中添加ngc文件,ngc文件就是core ge ...
- TCP协议承载的DNS报文,DNS报文首部前多出两个字节的DNS报文长度字段,是何意义?
一.TCP报文头部简介 ●源.目标端口号字段:占16比特.TCP协议通过使用"端口"来标识源端和目标端的应用进程.端口号可以使用0到65535之间的任何数字.在收到服务请求时,操作 ...
- JQuery 表格 隔行换色 和鼠标滑过的样式
$(document).ready(function () { $(".Pub_TB tbody tr:even td").css("background-color&q ...
- Winform webBrowser 不跳转网页
private void webBrowser1_NewWindow(object sender, CancelEventArgs e) { string url = ((WebBrowser)sen ...
- CentOS 7 yum nginx MySQL PHP 简易环境搭建
用centos自带的yum源来安装nginx,mysql和php,超级方便,省去编译的麻烦,省去自己配置的麻烦,还能节省非常多的时间. 我们先把yum源换成国内的阿里云镜像源(当然不换也可以),先备份 ...
- lower_bound和upper_bound
lower_bound:返回大于或等于val的第一个元素位置 upper_bound:返回大于val的第一个元素位置 两个函数用的都是二分查找
- Nginx的平滑重启和平滑升级
一,Nginx的平滑重启如果改变了Nginx的配置文件(nginx.conf),想重启Nginx,可以发送系统信号给Nginx主进程的方式来进行.在重启之前,要确认Nginx配置文件的语法是正确的. ...