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串然后首位和末位固定不变,从第二项开 ...
随机推荐
- SVG之初识
什么是SVG? 也许现在很多人都听说过SVG的人比较多,但不一定了解什么是SVG:SVG(Scalable Vector Graphics 一大串看不懂的英文)可伸缩矢量图形,它是用XML格式来定义用 ...
- Python类和实例
面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法,但各自的数据可 ...
- ASP.NET Ajax核心对象
本章学习目标 主要掌握AJAX的基本概念和实现机制,学习并创建XMLHttpRequest对象,使用XMLHttpRequestObject对象获取服务器端的数据 主要内容如下,请点击ASP.NET ...
- js利用数组length属性清空和截短数组
1.使用length清空数组: 代码如下 复制代码 <script> var arr1 = ['aaa','bbbb','http://www.111cn.net']; ale ...
- 基于 unity ngui 上的滚动加载__UiVirtual
在游戏里面经常会有背包,好友,对话,这样的列表.当列表的内容多了,如果一打开界面就对所有内容进行实例化,会消耗大量的性能,且会造成UI上的卡顿. 于是便需要,在列表里面只实例化屏幕上可见的item.屏 ...
- zedboard启动过程分析
1.经过几天的努力看懂了zedboard的部分启动过程 陆书与何宾老师的书上都说到了BootRom , 这个是被称为第0阶段启动引导,这阶段的代码在上电或者热复位时执行,启动代码不可更改,这是比我们所 ...
- 理解 pkg-config 工具
引用了别人的文章:http://www.chenjunlu.com/2011/03/understanding-pkg-config-tool/ 你在 Unix 或 Linux 下开发过软件吗?写完一 ...
- 汇编语言-求X的阶乘
1. 题目:求X的阶乘值 2. 要求:输入一个整型数(不超过10),求出其阶乘值后输出,求阶乘的算法用子程序来实现. 3. 提示:可以用递归来实现,也可以用简单的循环来实现. 这里使用循环来实现: 对 ...
- jquery文字上下滚动的实现方法
jquery实现文字上下滚动的方法. 代码: //上下滚动var textRoll=function(){$('#notice p:last').css({'height':'0px','opacit ...
- 关于无限分类的树状输出(id,name,pid)类型的
首先创建无限分类的数据表,我这里采用的是id.name.pid这种类型(当然还有很多种无限分类的方式了,比如:id.name.pid.path.left.right左右节点的形式) CREATE TA ...