Pagini recente » Cod sursa (job #2423773) | Borderou de evaluare (job #1193670) | Cod sursa (job #2516572) | Cod sursa (job #350579) | Cod sursa (job #2654636)
#include <bits/stdc++.h>
using namespace std;
ifstream in("volum.in");
ofstream out("volum.out");
const int NMAX=760;
int a[NMAX][NMAX],hmax[NMAX][NMAX];
long long dx[]={-1,1,0,0},dy[]={0,0,-1,1},n,m,rasp,h_extins;
bool seen[NMAX][NMAX];
struct poz{
int x,y,h;
bool operator <(const poz& aux)const{
return aux.h<h;
}
};
bool ok(int x,int y){
return x>0 && x<=n && y>0 && y<=m;
}
int h_max(int x,int y){
int maxim=0;
if(x==1 || x==n || y==1 || y==m) return 0;
for(int k=0;k<4;k++) {
int xf=x+dx[k],yf=y+dy[k];
if(ok(xf,yf))
if(hmax[xf][yf]>0 && hmax[xf][yf]<=a[x][y])
return 0;
else
maxim=max(maxim,hmax[xf][yf]-a[x][y]);
}
return maxim;
}
priority_queue<poz>pq;
int main()
{
in>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
in>>a[i][j];
if(i==1 || i==n || j==m || j==1) pq.push({i,j,a[i][j]});
}
while(!pq.empty()){
int x=pq.top().x,y=pq.top().y,h=pq.top().h;
pq.pop();
h_extins=h_max(x,y);
rasp+=h_extins;
hmax[x][y]=h+h_extins;
for(int k=0;k<4;k++){
int xf=x+dx[k],yf=y+dy[k];
if(ok(xf,yf) && !seen[xf][yf]){
seen[xf][yf]=1;
pq.push({xf,yf,a[xf][yf]});
}
}
}
out<<rasp;
return 0;
}