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串然后首位和末位固定不变,从第二项开 ...
随机推荐
- C#中如何查找Dictionary中的重复值
简介 在这篇帮助文档中,我将向你展示如何实现c#里字典中重复值的查找.你知道的对于一个老鸟来说,这是非常简单的代码.但是尽管如此,这也是一篇对c#初学者非常有用的帮助文档. 背景 多数程序员对小型数据 ...
- (转)[转]大数据时代的 9 大Key-Value存储数据库
在过去的十年中,计算世界已经改变.现在不仅在大公司,甚至一些小公司也积累了TB量级的数据.各种规模的组织开始有了处理大数据的需求,而目前关系型数据库在可缩放方面几乎已经达到极限. 一个解决方案是使用键 ...
- 8款超酷而实用的CSS3按钮动画
1.CSS3分享按钮动画特效 这是一款基于CSS3的社会化分享按钮,按钮非常简单,提供了分享到twitter.facebook.youtube等大型社交网站.每一个分享按钮都有个大社交网站的Logo图 ...
- JS函数式编程【译】第二章总结
- 济南学习 Day 5 T1 pm
欧拉函数(phi)题目描述: 已知(N),求phi(N). 输入说明: 正整数N. 输出说明: 输出phi(N). 样例输入: 8 样例输出: 4 数据范围: 对于20%的数据,N<=10^5 ...
- 将mysql的查询结果输出到文件
在sql命令中我们可以查询到前数行的表,同时也可以将查询结果输出到txt文档 语句:select * from tablename into outfile 'filename.txt'; 例如:se ...
- 【风马一族_Android】Android 前端内容
Android 前端内容 4.1 View 类概述 4.1.1 关于 View //类型说明 view(视图)指的是用户界面组件的基本构建基块.一个视图占据屏幕上的矩形区域,负责绘图和事件处理.视图是 ...
- js设计模式(8)---享元模式
0.前言 今天总结了四种设计模式,到现在有点精疲力尽了,但是还是有不少收获,很开心自己有掌握了新的东西,今天变得有了价值. 1.使用条件 1.1.网页中使用了大量资源密集型的对象: 1.2.这些对象中 ...
- 设置datagridview中button按钮的背景颜色
问题:DataGridViewButtonColumn()在datagridview中创建按钮列,如何设置按钮的背景颜色(不是单元格的背景颜色). 回答:可以在dataGridView1_CellPa ...
- gulp插件
gulp是趋势 gulp完全开发指南 => 快来换掉你的Grunt吧 gulp的工作流程:文件流--文件流--文件流......因为grunt操作会创建临时文件,会有频繁的IO操作,而gulp使 ...