1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| #include<bits/stdc++.h> using namespace std; inline int read(){int res=0,f=1;char ch=getchar();while(ch<'0'ch>'9'){if(ch=='-') f=-1;ch=getchar();}while(ch>='0'&&ch<='9') res=res*10+ch-'0',ch=getchar();return res*f;} inline void write(int x){if(x<0) putchar('-'),x=-x;if(x<10) putchar(x+'0');else write(x/10),putchar(x%10+'0');} int n,m,a[110][110],b[110][110],vis[110][110],ans; struct node{int x,y,h;bool operator<(const node &t)const{return h>t.h;}}; priority_queue<node> q; const int dx[]={0,0,1,-1}, dy[]={1,-1,0,0}; inline void spfa(){ while(!q.empty()){ node u=q.top();q.pop(); int x=u.x,y=u.y,h=u.h; for(int i=0;i<4;i++){ int xx=x+dx[i],yy=y+dy[i]; if(xx>=1&&xx<=n&&yy>=1&&yy<=m){ if(vis[xx][yy]) continue ; a[xx][yy]=max(a[xx][yy],h); vis[xx][yy]=1,q.push((node){xx,yy,a[xx][yy]}); } } } } int main(){ n=read();m=read(); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) b[i][j]=a[i][j]=read(); while(!q.empty()) q.pop(); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(i==1j==1i==nj==m) q.push((node){i,j,a[i][j]}),vis[i][j]=1; spfa(); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) ans+=a[i][j]-b[i][j]; return write(ans),putchar('\n'),0; }
|