Codeforces 591E Three States【优先队列Bfs+思维】
来源:互联网 发布:php的工厂模式 编辑:程序博客网 时间:2024/06/05 21:10
题目大意:
现在我们有三个国家,每个国家都是一个联通块,用数字1、2和3来表示。
现在我们希望在点的位子上建立路,使得三个国家连通起来,现在问需要建立路的最小个数。
思路:
因为只有三个国家,所以我们用每个国家求一次单源最短路,记录dist【i】【x】【y】表示i号国家想要和点(x,y)进行连通的话,需要建立的路的个数。
那么对应,我们只要O(nm)的去枚举一个交汇点,那么dist【1】【x】【y】+dist【2】【x】【y】+dist【3】【x】【y】就是答案,如果点(x,y)是点的话,那么对应还要减去2.
我们去Bfs的时候,注意到达每个点的时候,需要求最短路,所以我们这里直接用队列Bfs的话可能会TLE,用优先队列优化一下就不会了TAT、
Ac代码:
#include<stdio.h>#include<queue>#include<string.h>#include<algorithm>#include<stack>using namespace std;struct node{ int x,y,step; bool friend operator <(node a ,node b) { return a.step>b.step; }}now,nex;char a[1050][1050];int dodo[400];int dist[5][1050][1050];int vis[1050][1050];int fx[4]={0,0,1,-1};int fy[4]={1,-1,0,0};int n,m;void Bfs(int x,int y,int dd){ memset(vis,0,sizeof(vis)); priority_queue<node>s; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { dist[dd][i][j]=0x3f3f3f3f; if(a[i][j]-'0'==dd) { now.x=i; now.y=j; now.step=0; s.push(now); dist[dd][i][j]=0; } } } while(!s.empty()) { now=s.top(); vis[now.x][now.y]=0; s.pop(); for(int i=0;i<4;i++) { nex.x=now.x+fx[i]; nex.y=now.y+fy[i]; nex.step=now.step; if(nex.x>=0&&nex.x<n&&nex.y>=0&&nex.y<m&&a[nex.x][nex.y]!='#') { if(a[nex.x][nex.y]=='.') { nex.step++; } if(dist[dd][nex.x][nex.y]>nex.step) { dist[dd][nex.x][nex.y]=nex.step; if(vis[nex.x][nex.y]==0) { s.push(nex); } } } } }}int main(){ while(~scanf("%d%d",&n,&m)) { memset(dodo,0,sizeof(dodo)); for(int i=0;i<n;i++)scanf("%s",a[i]); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(dodo[a[i][j]]==0&&a[i][j]>='1'&&a[i][j]<='3') { Bfs(i,j,a[i][j]-'0');dodo[a[i][j]]=1; } } } int output=0x3f3f3f3f; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(a[i][j]!='#') { int flag=0; int ans=0; for(int dd=1;dd<=3;dd++) { if(dist[dd][i][j]==0x3f3f3f3f)flag=1; ans+=dist[dd][i][j]; } if(flag==1)continue; if(a[i][j]=='.')ans-=2; output=min(output,ans); } } } if(output==0x3f3f3f3f)printf("-1\n"); else printf("%d\n",output); }}
阅读全文
0 0
- Codeforces 591E Three States【优先队列Bfs+思维】
- codeforces 591 E. Three States
- Codeforces 590C Three States(bfs)
- BFS Codeforces591E Three States
- Codeforces Round #327 & problem - 590C - C. Three States - BFS
- Codeforces Round #327 (Div. 2)E. Three States
- HDU 3442 Three Kingdoms BFS + 优先队列
- Three Kingdoms(BFS+优先队列)
- HDU 3442 Three Kingdoms(BFS + 优先队列)
- Codeforces 767E 优先队列
- 【Codeforces Round #327 (Div. 2)E】【最短路】 Three States 块联通
- codeforces 507E (dijkstra+优先队列)
- codeforces 731E (优先队列 DP)
- CodeForces 732E - Sockets(优先队列)
- codeforces 603E(LCT+优先队列)
- CodeForces 825E Educational #25 E:拓扑排序+优先队列
- Gym 101572E Emptying Baltic【优先队列+BFS】
- Codeforces 190E Counter Attack【思维+Bfs】好题!
- Spring Boot 中文乱码解决
- U盘格式化后容量变小了怎么恢复教程
- socketio-cpp-lib使用
- 一种C#读写二进制文件的通用方法
- Minicom 使用初步
- Codeforces 591E Three States【优先队列Bfs+思维】
- Spring Boot自定义PropertySourceLoader
- Zookeeper acl
- 2017找工作总结——机会总是留给有准备的人
- 7.0系统WindowManager使用问题
- Kruscal算法的简单介绍
- 易云股份推出CDN加速服务,极速稳定高性价比
- Mysql中事务控制及特点
- CGI详解