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串然后首位和末位固定不变,从第二项开 ...
随机推荐
- 清除windows的EFS加密
所使用软件为aefsdr_setup_en,搜索名为advanced.efs.data.recovery 1. 创建需要加密的文件 2. 进行加密 ...
- IOS版应用商店应用源码
app商店 swift版 用swift编写的 应用商店 支持iPad iPhone利用了ios8过渡动画 支持横竖屏操作 源码下载: http://code.662p.com/view/11384.h ...
- js正则表达式的验证示例
//验证邮箱的JS正则 <script type="text/javascript"> $(function() { $("#inputemail" ...
- (转)Yale CAS + .net Client 实现 SSO(4)
第一部分:安装配置 Tomcat 第二部分:安装配置 CAS 第三部分:实现 ASP.NET WebForm Client 第四部分:实现基于数据库的身份验证 1.下载 Microsoft JDBC ...
- 最新13个加速 Web 开发的框架和工具
我们为开发人员挑选了15个最新的Web开发框架,你肯定尝试一下这些新鲜的框架,有的可能略微复杂,有的提供了很多的配置选项,也有一些窗口小部件和界面交互的选择.他们将帮助你创建更优秀的网站,提供给用户更 ...
- http请求访问过程
流程图(理解): 域名的作用: 对外,供访问 对内,提供域名与目录的对应关系 步骤说明: http://www.163.com/index.html 第1步:在本机的hosts文件中查找域名与IP的对 ...
- Model Builder中Table2Table中字段映射的问题
ArcGIS10中使用过程中,Bug不少.尽管有了SP3,但模型耦合的深层次的应用中还是错误不少.目前只是遇到一个,利用躲避的方法解决一个.例如,从NetCDF中抽出的数据表,必须在内存和数据库中都存 ...
- Chipscope 仿真VmodCAM IIC程序
Chipscope 仿真VmodCAM IIC程序: 目的:熟悉EDK中建立chipscope 注意:zedboard使用digilent USB下载时,chipscope不能和SDK同时使用,否则芯 ...
- ADO.NET笔记——使用连接池
相关知识: 连接池的意义: 应用程序往往涉及大量的,并发的数据访问操作 数据库服务器能够同时维系的连接数量非常有限.如果某个数据库访问操作不及时关闭连接,就会减少其他调用对数据库访问的机会.因此,一般 ...
- 【转】理解JavaScript之闭包
闭包(closure)是掌握Javascript从人门到深入一个非常重要的门槛,它是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现.下面写下我的学习笔记~ 闭包-无处不 ...