优先队列
来源:互联网 发布:淘宝卖家7天不发货 编辑:程序博客网 时间:2024/05/22 13:59
决定桶的容量是那个最短板#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;typedef long long LL;int n,m;LL ans;int a[350][350];int book[350][350];int _next[4][2]={0,1,1,0,0,-1,-1,0};struct Node{ int x,y,value; friend bool operator < (Node p,Node q) { return p.value>q.value; }};priority_queue<Node>que;void dfs(int x,int y,int w){ que.pop(); for(int i=0;i<4;i++) { int tx=x+_next[i][0],ty=y+_next[i][1]; if(tx<1||tx>n||ty<1||ty>m||book[tx][ty]) continue; book[tx][ty]=1; if(a[tx][ty]<w) { ans=ans+w-a[tx][ty]; Node temp;temp.x=tx,temp.y=ty,temp.value=w; que.push(temp); } else { Node temp;temp.x=tx,temp.y=ty,temp.value=a[tx][ty]; que.push(temp); } } if(!que.empty()) dfs(que.top().x,que.top().y,que.top().value);}int main(){ while(scanf("%d%d",&m,&n)!=EOF) { memset(book,0,sizeof(book)); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&a[i][j]); while(!que.empty()) que.pop(); ans=0; for(int j=2;j<=m-1;j++) { Node temp; temp.x=1,temp.y=j,temp.value=a[1][j]; que.push(temp); temp.x=n,temp.y=j,temp.value=a[n][j]; que.push(temp); book[1][j]=book[n][j]=1; } for(int i=2;i<=n-1;i++) { Node temp; temp.x=i,temp.y=1,temp.value=a[i][1]; que.push(temp); temp.x=i,temp.y=m,temp.value=a[i][m]; que.push(temp); book[i][1]=book[i][m]=1; } book[1][1]=book[n][1]=book[1][m]=book[n][m]=1; if(!que.empty()) //poj要加这个,,数据违规给小于3的数据。。不加re dfs(que.top().x,que.top().y,que.top().value); printf("%lld\n",ans); } return 0;}
阅读全文
0 0
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 矩乘dp 总结
- Java学习笔记(七)——继承
- nodejs 实践:express 最佳实践(二) 中间件
- Makefile中的-Wall -O2 -Os -g等选项介绍
- Kafka (一) 概述
- 优先队列
- JavaScript常用的数组API和对象API
- 欢迎使用CSDN-markdown编辑器
- Unity3D设计模式之工厂模式(4)
- Qt学习笔记之QLabel、QLineEdit控件
- solr 6.3 入门(二)
- 深入浅出开源性能测试工具Locust
- 安卓mvp学习
- bzoj4818 [Sdoi2017]序列计数(矩阵)