题意:
思路:
搞一个priority_queue 先把边界加进去 不断取最小的 向中间扩散
//By SiriusRen
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
#define int long long
struct Node{int h,x,y;Node(int a,int b,int c){h=a,x=b,y=c;}};
priority_queue<Node>pq;
bool operator <(Node a,Node b){return a.h>b.h;}
int n,m,ans,a[305][305],xx[]={1,-1,0,0},yy[]={0,0,1,-1};
bool vis[305][305];
signed main(){
memset(vis,1,sizeof(vis));
scanf("%lld%lld",&m,&n);
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)scanf("%lld",&a[i][j]),vis[i][j]=0;
for(int i=1;i<=n;i++)pq.push(Node(a[i][1],i,1)),pq.push(Node(a[i][m],i,m)),vis[i][m]=vis[i][1]=1;
for(int i=2;i<=m;i++)pq.push(Node(a[1][i],1,i)),pq.push(Node(a[n][i],n,i)),vis[n][i]=vis[1][i]=1;
while(!pq.empty()){
Node t=pq.top();pq.pop();
for(int i=0;i<4;i++){
int dx=t.x+xx[i],dy=t.y+yy[i];
if(!vis[dx][dy]){
if(a[dx][dy]<t.h)ans+=t.h-a[dx][dy],pq.push(Node(t.h,dx,dy));
else pq.push(Node(a[dx][dy],dx,dy));
vis[dx][dy]=1;
}
}
}
printf("%lld\n",ans);
}