C. Three States
2015-10-27 19:14
302 查看
题目描述:
C. Three Statestime limit per test5 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
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.
Input
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.
Output
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.
Sample test(s)
input
4 5
11..2
..22
.323
.#333output
2
input
1 5
1#2#3
output
-1
题解:
关键是3个点来联通.3个点来联通的话,如果实在中间相遇,一定会有且仅有一个公共的相遇点. 而在这道题就算是两两建边,也可以看做是有公共点.所以暴力枚举唯一的公共点,然后先bfs预处理出1 2 3 到这个格子用的新建的路数. 注意,bfs不一定路会+1,所以如果不加1我们用双端队列,放在双端队列前面.(用的spfa没有被卡…..)重点:
3个点的联通公共相遇的话有且只有一个公共点.而不会是一段公共线段.代码:
#include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; typedef long long ll; int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1}; const int maxn = 1000+10; const int INF = 1e9; int f[4][maxn][maxn]; struct info { int x, y; info(int _x = 0, int _y = 0) { x = _x; y = _y; } }; char s[maxn][maxn]; int a[maxn][maxn]; int inq[maxn][maxn]; int n, m; int check(int x, int y) { if(x >= 1 && x<=n&&y>=1&&y<=m) return 1; return 0; } queue<info> que; void gao(int key, int f[][maxn]) { memset(inq, 0, sizeof(inq)); for(int i = 1; i<=n; i++) { for(int j = 1; j<=m; j++) { f[i][j] = INF; } } while(!que.empty()) { que.pop(); } for(int i = 1; i<=n; i++) { for(int j = 1; j<=m; j++) { if(a[i][j]==key) { f[i][j] = 0; inq[i][j] = 1; que.push(info(i, j)); } } } while(!que.empty()) { info t = que.front(); que.pop(); int x = t.x, y = t.y; inq[x][y] = 0; for(int k = 0; k<4; k++) { int nx = x+dx[k], ny = y+dy[k]; if(check(nx, ny) && a[nx][ny]!=-1) { int tmp = f[x][y]; if(a[nx][ny]==0) { tmp++; } if(tmp < f[nx][ny]) { f[nx][ny] = tmp; if(inq[nx][ny]==0) { que.push(info(nx, ny)); inq[nx][ny] = 1; } } } } } } void solve() { gao(1, f[1]); gao(2, f[2]); gao(3, f[3]); // for(int k = 1;k<=3;k++) // { // for(int i = 1;i<=n;i++) // { // for(int j = 1;j<=m;j++) // { // printf("%d %d %d -- %d\n", k, i, j, f[k][i][j]); // } // } // } ll ans = INF; for(int i = 1;i<=n;i++) { for(int j = 1;j<=m;j++) { ll tmp = 0; for(int k = 1;k<=3;k++) { tmp += f[k][i][j]; } if(a[i][j] == 0) { tmp -= 2; } // if(tmp < ans) // { // printf("%d %d --- %d\n", i, j, tmp); // } ans = min(tmp, ans); } } if(ans < INF) printf("%I64d\n", ans); else printf("-1\n"); } int main() { //freopen("in.txt", "r", stdin); while(scanf("%d%d", &n,&m) != EOF) { for(int i = 1;i<=n;i++) { scanf("%s", s[i]+1); for(int j = 1;j<=m;j++) { if(s[i][j]=='1') a[i][j] = 1; if(s[i][j]=='2') a[i][j] = 2; if(s[i][j]=='3') a[i][j] = 3; if(s[i][j]=='#') a[i][j] = -1; if(s[i][j]=='.') a[i][j] = 0; } } solve(); } return 0; }
相关文章推荐
- Java学习日记 集合
- swift-UITableView的基本使用
- 代码评比结果的反思
- Oracle GoldenGate (以下简称ogg)在异种移植os同一种db之间的数据同步。
- 读书笔记cocos2d-x之代码风格
- 图像处理(六)递归双边滤波磨皮
- CDMA2000各种信令流程
- 【LWJGL官方教程】Introduction 入门
- linux运维要掌握的工具
- 欢迎使用CSDN-markdown编辑器
- Python: 实现bitmap数据结构
- 024 Swap Nodes in Pairs [Leetcode]
- smb
- lintcode 中等题:Majority number II 主元素 II
- 合并两个排序链表(LintCode)
- C#创建桌面快捷方式
- 利用c语言求出0~999之间的所有“水仙花数”并输出
- 有关电子商务平台的个人学习总结
- 语音通话中talkspurt的介绍
- 工厂模式